Moved OAuth 2.0 samples up to the top level.
diff --git a/samples/urlshortener/main.py b/samples/urlshortener/main.py
index 4609c9f..62945c5 100644
--- a/samples/urlshortener/main.py
+++ b/samples/urlshortener/main.py
@@ -10,19 +10,49 @@
__author__ = 'jcgregorio@google.com (Joe Gregorio)'
-from apiclient.discovery import build
-
+import gflags
+import httplib2
+import logging
import pprint
+import sys
-# Uncomment the next two lines to get very detailed logging
-#import httplib2
-#httplib2.debuglevel = 4
+from apiclient.discovery import build
+from oauth2client.file import Storage
+from oauth2client.client import OAuth2WebServerFlow
+from oauth2client.client import AccessTokenCredentials
+from oauth2client.tools import run
+
+FLAGS = gflags.FLAGS
+FLOW = OAuth2WebServerFlow(
+ client_id='433807057907.apps.googleusercontent.com',
+ client_secret='jigtZpMApkRxncxikFpR+SFg',
+ scope='https://www.googleapis.com/auth/urlshortener',
+ user_agent='urlshortener-cmdline-sample/1.0')
+
+gflags.DEFINE_enum('logging_level', 'ERROR',
+ ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
+ 'Set the level of logging detail.')
-def main():
+def main(argv):
+ try:
+ argv = FLAGS(argv)
+ except gflags.FlagsError, e:
+ print '%s\\nUsage: %s ARGS\\n%s' % (e, argv[0], FLAGS)
+ sys.exit(1)
+
+ logging.getLogger().setLevel(getattr(logging, FLAGS.logging_level))
+
+ storage = Storage('urlshortener.dat')
+ credentials = storage.get()
+ if credentials is None or credentials.invalid == True:
+ credentials = run(FLOW, storage)
+
+ http = httplib2.Http()
+ http = credentials.authorize(http)
# Build the url shortener service
- service = build("urlshortener", "v1",
+ service = build("urlshortener", "v1", http=http,
developerKey="AIzaSyDRRpR3GS1F1_jKNNM9HCNd2wJQyPG3oN0")
url = service.url()
@@ -37,5 +67,6 @@
resp = url.get(shortUrl=shortUrl).execute()
pprint.pprint(resp)
+
if __name__ == '__main__':
- main()
+ main(sys.argv)