Joe Gregorio | 0802a17 | 2010-10-26 16:23:00 -0400 | [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 | |
| 15 | from apiclient.discovery import build |
| 16 | |
| 17 | import httplib2 |
| 18 | import pickle |
| 19 | |
Joe Gregorio | 9ce4b62 | 2011-02-17 15:32:11 -0500 | [diff] [blame] | 20 | from apiclient.discovery import build |
| 21 | from apiclient.oauth import FlowThreeLegged |
| 22 | from apiclient.ext.authtools import run |
| 23 | from apiclient.ext.file import Storage |
| 24 | |
Joe Gregorio | 0802a17 | 2010-10-26 16:23:00 -0400 | [diff] [blame] | 25 | # Uncomment to get detailed logging |
| 26 | # httplib2.debuglevel = 4 |
| 27 | |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 28 | |
Joe Gregorio | 0802a17 | 2010-10-26 16:23:00 -0400 | [diff] [blame] | 29 | def main(): |
Joe Gregorio | fffa7d7 | 2011-02-18 17:20:39 -0500 | [diff] [blame] | 30 | storage = Storage('latitude.dat') |
| 31 | credentials = storage.get() |
Joe Gregorio | a0a52e4 | 2011-02-17 17:13:26 -0500 | [diff] [blame] | 32 | if credentials is None or credentials.invalid == True: |
Joe Gregorio | 9ce4b62 | 2011-02-17 15:32:11 -0500 | [diff] [blame] | 33 | auth_discovery = build("latitude", "v1").auth_discovery() |
| 34 | flow = FlowThreeLegged(auth_discovery, |
| 35 | # You MUST have a consumer key and secret tied to a |
| 36 | # registered domain to use the latitude API. |
| 37 | # |
| 38 | # https://www.google.com/accounts/ManageDomains |
| 39 | consumer_key='REGISTERED DOMAIN NAME', |
| 40 | consumer_secret='KEY GIVEN DURING REGISTRATION', |
| 41 | user_agent='google-api-client-python-latitude/1.0', |
| 42 | domain='REGISTERED DOMAIN NAME', |
| 43 | scope='https://www.googleapis.com/auth/latitude', |
| 44 | xoauth_displayname='Google API Latitude Example', |
| 45 | location='current', |
| 46 | granularity='city' |
| 47 | ) |
| 48 | |
Joe Gregorio | fffa7d7 | 2011-02-18 17:20:39 -0500 | [diff] [blame] | 49 | credentials = run(flow, storage) |
Joe Gregorio | 0802a17 | 2010-10-26 16:23:00 -0400 | [diff] [blame] | 50 | |
| 51 | http = httplib2.Http() |
| 52 | http = credentials.authorize(http) |
| 53 | |
Joe Gregorio | 1ae3e74 | 2011-02-25 15:17:14 -0500 | [diff] [blame^] | 54 | service = build("latitude", "v1", http=http) |
Joe Gregorio | 0802a17 | 2010-10-26 16:23:00 -0400 | [diff] [blame] | 55 | |
| 56 | body = { |
| 57 | "data": { |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 58 | "kind": "latitude#location", |
| 59 | "latitude": 37.420352, |
| 60 | "longitude": -122.083389, |
| 61 | "accuracy": 130, |
| 62 | "altitude": 35 |
Joe Gregorio | 0802a17 | 2010-10-26 16:23:00 -0400 | [diff] [blame] | 63 | } |
| 64 | } |
Joe Gregorio | 1ae3e74 | 2011-02-25 15:17:14 -0500 | [diff] [blame^] | 65 | print service.currentLocation().insert(body=body).execute() |
Joe Gregorio | 0802a17 | 2010-10-26 16:23:00 -0400 | [diff] [blame] | 66 | |
| 67 | if __name__ == '__main__': |
| 68 | main() |