Joe Gregorio | 292b9b8 | 2011-01-12 11:36:11 -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 Buzz. |
| 7 | |
| 8 | Command-line application that retrieves the users |
| 9 | latest content and then adds a new entry. |
| 10 | """ |
| 11 | |
| 12 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 13 | |
Joe Gregorio | fffa7d7 | 2011-02-18 17:20:39 -0500 | [diff] [blame] | 14 | from apiclient.discovery import build |
Joe Gregorio | 292b9b8 | 2011-01-12 11:36:11 -0500 | [diff] [blame] | 15 | from apiclient.discovery import build_from_document |
Joe Gregorio | fffa7d7 | 2011-02-18 17:20:39 -0500 | [diff] [blame] | 16 | from apiclient.oauth import FlowThreeLegged |
| 17 | from apiclient.ext.authtools import run |
| 18 | from apiclient.ext.file import Storage |
| 19 | from apiclient.oauth import CredentialsInvalidError |
Joe Gregorio | 292b9b8 | 2011-01-12 11:36:11 -0500 | [diff] [blame] | 20 | |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 21 | import gflags |
| 22 | import sys |
Joe Gregorio | 292b9b8 | 2011-01-12 11:36:11 -0500 | [diff] [blame] | 23 | import httplib2 |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 24 | import logging |
| 25 | import os |
Joe Gregorio | 292b9b8 | 2011-01-12 11:36:11 -0500 | [diff] [blame] | 26 | import pprint |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 27 | import sys |
Joe Gregorio | 292b9b8 | 2011-01-12 11:36:11 -0500 | [diff] [blame] | 28 | |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 29 | FLAGS = gflags.FLAGS |
| 30 | |
| 31 | gflags.DEFINE_enum('logging_level', 'ERROR', |
| 32 | ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], |
| 33 | 'Set the level of logging detail.') |
Joe Gregorio | 292b9b8 | 2011-01-12 11:36:11 -0500 | [diff] [blame] | 34 | |
| 35 | |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 36 | def main(argv): |
| 37 | try: |
| 38 | argv = FLAGS(argv) |
| 39 | except gflags.FlagsError, e: |
| 40 | print '%s\\nUsage: %s ARGS\\n%s' % (e, argv[0], FLAGS) |
| 41 | sys.exit(1) |
| 42 | |
| 43 | logging.getLogger().setLevel(getattr(logging, FLAGS.logging_level)) |
| 44 | |
Joe Gregorio | fffa7d7 | 2011-02-18 17:20:39 -0500 | [diff] [blame] | 45 | storage = Storage('buzz.dat') |
| 46 | credentials = storage.get() |
| 47 | if credentials is None or credentials.invalid == True: |
| 48 | buzz_discovery = build("buzz", "v1").auth_discovery() |
Joe Gregorio | fffa7d7 | 2011-02-18 17:20:39 -0500 | [diff] [blame] | 49 | flow = FlowThreeLegged(buzz_discovery, |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 50 | consumer_key='anonymous', |
| 51 | consumer_secret='anonymous', |
| 52 | user_agent='python-buzz-sample/1.0', |
| 53 | domain='anonymous', |
| 54 | scope='https://www.googleapis.com/auth/buzz', |
| 55 | xoauth_displayname='Google API Client Example App') |
Joe Gregorio | fffa7d7 | 2011-02-18 17:20:39 -0500 | [diff] [blame] | 56 | credentials = run(flow, storage) |
Joe Gregorio | 292b9b8 | 2011-01-12 11:36:11 -0500 | [diff] [blame] | 57 | |
| 58 | http = httplib2.Http() |
| 59 | http = credentials.authorize(http) |
| 60 | |
| 61 | # Load the local copy of the discovery document |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 62 | f = file(os.path.join(os.path.dirname(__file__), "buzz.json"), "r") |
Joe Gregorio | 292b9b8 | 2011-01-12 11:36:11 -0500 | [diff] [blame] | 63 | discovery = f.read() |
| 64 | f.close() |
| 65 | |
| 66 | # Optionally load a futures discovery document |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 67 | f = file(os.path.join(os.path.dirname(__file__), "../../apiclient/contrib/buzz/future.json"), "r") |
Joe Gregorio | 292b9b8 | 2011-01-12 11:36:11 -0500 | [diff] [blame] | 68 | future = f.read() |
| 69 | f.close() |
| 70 | |
| 71 | # Construct a service from the local documents |
Joe Gregorio | 1ae3e74 | 2011-02-25 15:17:14 -0500 | [diff] [blame] | 72 | service = build_from_document(discovery, |
Joe Gregorio | 292b9b8 | 2011-01-12 11:36:11 -0500 | [diff] [blame] | 73 | base="https://www.googleapis.com/", |
| 74 | future=future, |
| 75 | http=http, |
| 76 | developerKey="AIzaSyDRRpR3GS1F1_jKNNM9HCNd2wJQyPG3oN0") |
Joe Gregorio | 1ae3e74 | 2011-02-25 15:17:14 -0500 | [diff] [blame] | 77 | activities = service.activities() |
Joe Gregorio | 292b9b8 | 2011-01-12 11:36:11 -0500 | [diff] [blame] | 78 | |
Joe Gregorio | fffa7d7 | 2011-02-18 17:20:39 -0500 | [diff] [blame] | 79 | try: |
| 80 | # Retrieve the first two activities |
| 81 | activitylist = activities.list( |
| 82 | max_results='2', scope='@self', userId='@me').execute() |
| 83 | print "Retrieved the first two activities" |
| 84 | except CredentialsInvalidError: |
| 85 | print 'Your credentials are no longer valid.' |
| 86 | print 'Please re-run this application to re-authorize.' |
Joe Gregorio | 292b9b8 | 2011-01-12 11:36:11 -0500 | [diff] [blame] | 87 | |
Joe Gregorio | 292b9b8 | 2011-01-12 11:36:11 -0500 | [diff] [blame] | 88 | |
| 89 | if __name__ == '__main__': |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 90 | main(sys.argv) |