3535
3636__author__ = 'jcgregorio@google.com (Joe Gregorio)'
3737
38- import gflags
39- import httplib2
40- import logging
41- import os
42- import pprint
4338import sys
4439
45- from apiclient .discovery import build
46- from oauth2client .file import Storage
47- from oauth2client .client import AccessTokenRefreshError
48- from oauth2client .client import flow_from_clientsecrets
49- from oauth2client .tools import run
50-
51-
52- FLAGS = 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>
58- CLIENT_SECRETS = 'client_secrets.json'
59-
60- # Helpful message to display in the browser if the CLIENT_SECRETS file
61- # is missing.
62- MISSING_CLIENT_SECRETS_MESSAGE = """
63- WARNING: Please configure OAuth 2.0
64-
65- To make this sample run you will need to populate the client_secrets.json file
66- found at:
67-
68- %s
69-
70- with 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.
75- FLOW = 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.
83- gflags .DEFINE_enum ('logging_level' , 'ERROR' ,
84- ['DEBUG' , 'INFO' , 'WARNING' , 'ERROR' , 'CRITICAL' ],
85- 'Set the level of logging detail.' )
86-
40+ from oauth2client import client
41+ from apiclient import sample_tools
8742
8843def 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.
102- storage = Storage ('latitude.dat' )
103- credentials = storage .get ()
104-
105- if credentials is None or credentials .invalid :
106- credentials = run (FLOW , storage )
107-
108- # Create an httplib2.Http object to handle our HTTP requests and authorize it
109- # with our good Credentials.
110- http = httplib2 .Http ()
111- http = credentials .authorize (http )
112-
113- service = build ("latitude" , "v1" , http = http )
44+ service , flags = sample_tools .init (
45+ argv , 'latitude' , 'v1' , __doc__ , __file__ ,
46+ scope = 'https://www.googleapis.com/auth/latitude.all.best' )
11447
11548 try :
11649 body = {
@@ -125,7 +58,7 @@ def main(argv):
12558
12659 print service .currentLocation ().insert (body = body ).execute ()
12760
128- except AccessTokenRefreshError :
61+ except client . AccessTokenRefreshError :
12962 print ("The credentials have been revoked or expired, please re-run"
13063 "the application to re-authorize" )
13164
0 commit comments