Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 1 | #!/usr/bin/python2.4 |
| 2 | # -*- coding: utf-8 -*- |
| 3 | # |
| 4 | # Copyright 2010 Google Inc. All Rights Reserved. |
| 5 | |
| 6 | """Simple command-line example for Latitude. |
| 7 | |
| 8 | Command-line application that sets the users |
| 9 | current location. |
| 10 | """ |
| 11 | |
| 12 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 13 | |
| 14 | import httplib2 |
| 15 | |
| 16 | from apiclient.discovery import build |
| 17 | from oauth2client.file import Storage |
| 18 | from oauth2client.client import OAuth2WebServerFlow |
| 19 | from oauth2client.tools import run |
Joe Gregorio | a0a52e4 | 2011-02-17 17:13:26 -0500 | [diff] [blame] | 20 | from apiclient.oauth import CredentialsInvalidError |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 21 | |
| 22 | # Uncomment to get detailed logging |
| 23 | #httplib2.debuglevel = 4 |
| 24 | |
| 25 | |
| 26 | def main(): |
Joe Gregorio | a0a52e4 | 2011-02-17 17:13:26 -0500 | [diff] [blame] | 27 | credentials = Storage('latitude.dat').get() |
Joe Gregorio | 9ce4b62 | 2011-02-17 15:32:11 -0500 | [diff] [blame] | 28 | if credentials is None or credentials.invalid: |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 29 | flow = OAuth2WebServerFlow( |
| 30 | client_id='433807057907.apps.googleusercontent.com', |
| 31 | client_secret='jigtZpMApkRxncxikFpR+SFg', |
| 32 | scope='https://www.googleapis.com/auth/latitude', |
| 33 | user_agent='latitude-cmdline-sample/1.0', |
| 34 | xoauth_displayname='Latitude Client Example App') |
| 35 | |
| 36 | credentials = run(flow, storage) |
| 37 | |
| 38 | http = httplib2.Http() |
| 39 | http = credentials.authorize(http) |
| 40 | |
Joe Gregorio | 1ae3e74 | 2011-02-25 15:17:14 -0500 | [diff] [blame^] | 41 | service = build("latitude", "v1", http=http) |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 42 | |
| 43 | body = { |
| 44 | "data": { |
| 45 | "kind": "latitude#location", |
| 46 | "latitude": 37.420352, |
| 47 | "longitude": -122.083389, |
| 48 | "accuracy": 130, |
| 49 | "altitude": 35 |
| 50 | } |
| 51 | } |
Joe Gregorio | a0a52e4 | 2011-02-17 17:13:26 -0500 | [diff] [blame] | 52 | try: |
Joe Gregorio | 1ae3e74 | 2011-02-25 15:17:14 -0500 | [diff] [blame^] | 53 | print service.currentLocation().insert(body=body).execute() |
Joe Gregorio | a0a52e4 | 2011-02-17 17:13:26 -0500 | [diff] [blame] | 54 | except CredentialsInvalidError: |
| 55 | print 'Your credentials are no longer valid.' |
| 56 | print 'Please re-run this application to re-authorize.' |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 57 | |
| 58 | if __name__ == '__main__': |
| 59 | main() |