blob: 3700f0146324755d0f28de92c16e5ea55f0a8984 [file] [log] [blame]
Joe Gregorio0802a172010-10-26 16:23:00 -04001#!/usr/bin/python2.4
2# -*- coding: utf-8 -*-
3#
Joe Gregorio88f699f2012-06-07 13:36:06 -04004# Copyright (C) 2012 Google Inc.
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
Joe Gregorio0802a172010-10-26 16:23:00 -040017
18"""Simple command-line example for Latitude.
19
20Command-line application that sets the users
21current location.
Joe Gregorio88f699f2012-06-07 13:36:06 -040022
23Usage:
24 $ python latitude.py
25
26You can also get help on all the command-line flags the program understands
27by running:
28
29 $ python latitude.py --help
30
31To get detailed log output run:
32
33 $ python latitude.py --logging_level=DEBUG
Joe Gregorio0802a172010-10-26 16:23:00 -040034"""
35
36__author__ = 'jcgregorio@google.com (Joe Gregorio)'
37
Joe Gregorio88f699f2012-06-07 13:36:06 -040038import gflags
Joe Gregorio0802a172010-10-26 16:23:00 -040039import httplib2
Joe Gregorio88f699f2012-06-07 13:36:06 -040040import logging
41import os
42import pprint
43import sys
Joe Gregorio0802a172010-10-26 16:23:00 -040044
Joe Gregorio9ce4b622011-02-17 15:32:11 -050045from apiclient.discovery import build
Joe Gregorio88f699f2012-06-07 13:36:06 -040046from oauth2client.file import Storage
47from oauth2client.client import AccessTokenRefreshError
48from oauth2client.client import flow_from_clientsecrets
49from oauth2client.tools import run
Joe Gregorio0802a172010-10-26 16:23:00 -040050
Joe Gregorioaf276d22010-12-09 14:26:58 -050051
Joe Gregorio88f699f2012-06-07 13:36:06 -040052FLAGS = gflags.FLAGS
53
54# CLIENT_SECRETS, name of a file containing the OAuth 2.0 information for this
55# application, including client_id and client_secret, which are found
56# on the API Access tab on the Google APIs
57# Console <http://code.google.com/apis/console>
58CLIENT_SECRETS = 'client_secrets.json'
59
60# Helpful message to display in the browser if the CLIENT_SECRETS file
61# is missing.
62MISSING_CLIENT_SECRETS_MESSAGE = """
63WARNING: Please configure OAuth 2.0
64
65To make this sample run you will need to populate the client_secrets.json file
66found at:
67
68 %s
69
70with information from the APIs Console <https://code.google.com/apis/console>.
71
72""" % os.path.join(os.path.dirname(__file__), CLIENT_SECRETS)
73
74# Set up a Flow object to be used if we need to authenticate.
75FLOW = flow_from_clientsecrets(CLIENT_SECRETS,
76 scope='https://www.googleapis.com/auth/latitude.all.best',
77 message=MISSING_CLIENT_SECRETS_MESSAGE)
78
79
80# The gflags module makes defining command-line options easy for
81# applications. Run this program with the '--help' argument to see
82# all the flags that it understands.
83gflags.DEFINE_enum('logging_level', 'ERROR',
84 ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
85 'Set the level of logging detail.')
86
87
88def main(argv):
89 # Let the gflags module process the command-line arguments
90 try:
91 argv = FLAGS(argv)
92 except gflags.FlagsError, e:
93 print '%s\\nUsage: %s ARGS\\n%s' % (e, argv[0], FLAGS)
94 sys.exit(1)
95
96 # Set the logging according to the command-line flag
97 logging.getLogger().setLevel(getattr(logging, FLAGS.logging_level))
98
99 # If the Credentials don't exist or are invalid run through the native client
100 # flow. The Storage object will ensure that if successful the good
101 # Credentials will get written back to a file.
Joe Gregoriofffa7d72011-02-18 17:20:39 -0500102 storage = Storage('latitude.dat')
103 credentials = storage.get()
Joe Gregorio9ce4b622011-02-17 15:32:11 -0500104
Joe Gregorio88f699f2012-06-07 13:36:06 -0400105 if credentials is None or credentials.invalid:
106 credentials = run(FLOW, storage)
Joe Gregorio0802a172010-10-26 16:23:00 -0400107
Joe Gregorio88f699f2012-06-07 13:36:06 -0400108 # Create an httplib2.Http object to handle our HTTP requests and authorize it
109 # with our good Credentials.
Joe Gregorio0802a172010-10-26 16:23:00 -0400110 http = httplib2.Http()
111 http = credentials.authorize(http)
112
Joe Gregorio1ae3e742011-02-25 15:17:14 -0500113 service = build("latitude", "v1", http=http)
Joe Gregorio0802a172010-10-26 16:23:00 -0400114
Joe Gregorio88f699f2012-06-07 13:36:06 -0400115 try:
116 body = {
117 "data": {
118 "kind": "latitude#location",
119 "latitude": 37.420352,
120 "longitude": -122.083389,
121 "accuracy": 130,
122 "altitude": 35
123 }
124 }
125
126 print service.currentLocation().insert(body=body).execute()
127
128 except AccessTokenRefreshError:
129 print ("The credentials have been revoked or expired, please re-run"
130 "the application to re-authorize")
Joe Gregorio0802a172010-10-26 16:23:00 -0400131
132if __name__ == '__main__':
Joe Gregorio88f699f2012-06-07 13:36:06 -0400133 main(sys.argv)