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 | |
| 21 | import httplib2 |
Joe Gregorio | 292b9b8 | 2011-01-12 11:36:11 -0500 | [diff] [blame] | 22 | import pprint |
| 23 | |
| 24 | # Uncomment the next line to get very detailed logging |
| 25 | #httplib2.debuglevel = 4 |
| 26 | |
| 27 | |
| 28 | def main(): |
Joe Gregorio | fffa7d7 | 2011-02-18 17:20:39 -0500 | [diff] [blame^] | 29 | storage = Storage('buzz.dat') |
| 30 | credentials = storage.get() |
| 31 | if credentials is None or credentials.invalid == True: |
| 32 | buzz_discovery = build("buzz", "v1").auth_discovery() |
| 33 | |
| 34 | flow = FlowThreeLegged(buzz_discovery, |
| 35 | consumer_key='anonymous', |
| 36 | consumer_secret='anonymous', |
| 37 | user_agent='python-buzz-sample/1.0', |
| 38 | domain='anonymous', |
| 39 | scope='https://www.googleapis.com/auth/buzz', |
| 40 | xoauth_displayname='Google API Client Example App') |
| 41 | |
| 42 | credentials = run(flow, storage) |
Joe Gregorio | 292b9b8 | 2011-01-12 11:36:11 -0500 | [diff] [blame] | 43 | |
| 44 | http = httplib2.Http() |
| 45 | http = credentials.authorize(http) |
| 46 | |
| 47 | # Load the local copy of the discovery document |
| 48 | f = file("buzz.json", "r") |
| 49 | discovery = f.read() |
| 50 | f.close() |
| 51 | |
| 52 | # Optionally load a futures discovery document |
| 53 | f = file("../../apiclient/contrib/buzz/future.json", "r") |
| 54 | future = f.read() |
| 55 | f.close() |
| 56 | |
| 57 | # Construct a service from the local documents |
| 58 | p = build_from_document(discovery, |
| 59 | base="https://www.googleapis.com/", |
| 60 | future=future, |
| 61 | http=http, |
| 62 | developerKey="AIzaSyDRRpR3GS1F1_jKNNM9HCNd2wJQyPG3oN0") |
| 63 | activities = p.activities() |
| 64 | |
Joe Gregorio | fffa7d7 | 2011-02-18 17:20:39 -0500 | [diff] [blame^] | 65 | try: |
| 66 | # Retrieve the first two activities |
| 67 | activitylist = activities.list( |
| 68 | max_results='2', scope='@self', userId='@me').execute() |
| 69 | print "Retrieved the first two activities" |
| 70 | except CredentialsInvalidError: |
| 71 | print 'Your credentials are no longer valid.' |
| 72 | print 'Please re-run this application to re-authorize.' |
Joe Gregorio | 292b9b8 | 2011-01-12 11:36:11 -0500 | [diff] [blame] | 73 | |
Joe Gregorio | 292b9b8 | 2011-01-12 11:36:11 -0500 | [diff] [blame] | 74 | |
| 75 | if __name__ == '__main__': |
| 76 | main() |