jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [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 | |
| 14 | from apiclient.discovery import build |
Joe Gregorio | d81ecb0 | 2011-02-15 17:46:47 -0500 | [diff] [blame] | 15 | from apiclient.oauth import FlowThreeLegged |
| 16 | from apiclient.ext.authtools import run |
Joe Gregorio | a0a52e4 | 2011-02-17 17:13:26 -0500 | [diff] [blame] | 17 | from apiclient.ext.file import Storage |
| 18 | from apiclient.oauth import CredentialsInvalidError |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 19 | |
| 20 | import httplib2 |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 21 | import pprint |
| 22 | |
| 23 | # Uncomment the next line to get very detailed logging |
Joe Gregorio | bc2ff9b | 2010-11-08 09:20:48 -0500 | [diff] [blame] | 24 | #httplib2.debuglevel = 4 |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 25 | |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 26 | |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 27 | def main(): |
Joe Gregorio | fffa7d7 | 2011-02-18 17:20:39 -0500 | [diff] [blame^] | 28 | storage = Storage('buzz.dat') |
| 29 | credentials = storage.get() |
Joe Gregorio | a0a52e4 | 2011-02-17 17:13:26 -0500 | [diff] [blame] | 30 | if credentials is None or credentials.invalid == True: |
Joe Gregorio | d81ecb0 | 2011-02-15 17:46:47 -0500 | [diff] [blame] | 31 | buzz_discovery = build("buzz", "v1").auth_discovery() |
| 32 | |
| 33 | flow = FlowThreeLegged(buzz_discovery, |
| 34 | consumer_key='anonymous', |
| 35 | consumer_secret='anonymous', |
Joe Gregorio | 9ce4b62 | 2011-02-17 15:32:11 -0500 | [diff] [blame] | 36 | user_agent='python-buzz-sample/1.0', |
Joe Gregorio | d81ecb0 | 2011-02-15 17:46:47 -0500 | [diff] [blame] | 37 | domain='anonymous', |
| 38 | scope='https://www.googleapis.com/auth/buzz', |
| 39 | xoauth_displayname='Google API Client Example App') |
| 40 | |
Joe Gregorio | fffa7d7 | 2011-02-18 17:20:39 -0500 | [diff] [blame^] | 41 | credentials = run(flow, storage) |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 42 | |
| 43 | http = httplib2.Http() |
| 44 | http = credentials.authorize(http) |
| 45 | |
Joe Gregorio | 88243be | 2011-01-11 14:13:06 -0500 | [diff] [blame] | 46 | p = build("buzz", "v1", http=http, |
| 47 | developerKey="AIzaSyDRRpR3GS1F1_jKNNM9HCNd2wJQyPG3oN0") |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 48 | activities = p.activities() |
| 49 | |
Joe Gregorio | a0a52e4 | 2011-02-17 17:13:26 -0500 | [diff] [blame] | 50 | try: |
| 51 | # Retrieve the first two activities |
| 52 | activitylist = activities.list( |
| 53 | max_results='2', scope='@self', userId='@me').execute() |
| 54 | print "Retrieved the first two activities" |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 55 | |
Joe Gregorio | a0a52e4 | 2011-02-17 17:13:26 -0500 | [diff] [blame] | 56 | # Retrieve the next two activities |
| 57 | if activitylist: |
| 58 | activitylist = activities.list_next(activitylist).execute() |
| 59 | print "Retrieved the next two activities" |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 60 | |
Joe Gregorio | a0a52e4 | 2011-02-17 17:13:26 -0500 | [diff] [blame] | 61 | # Add a new activity |
| 62 | new_activity_body = { |
| 63 | "data": { |
| 64 | 'title': 'Testing insert', |
| 65 | 'object': { |
| 66 | 'content': |
| 67 | u'Just a short note to show that insert is working. ☄', |
| 68 | 'type': 'note'} |
Joe Gregorio | bc2ff9b | 2010-11-08 09:20:48 -0500 | [diff] [blame] | 69 | } |
Joe Gregorio | a0a52e4 | 2011-02-17 17:13:26 -0500 | [diff] [blame] | 70 | } |
| 71 | activity = activities.insert( |
| 72 | userId='@me', body=new_activity_body).execute() |
| 73 | print "Added a new activity" |
| 74 | |
| 75 | activitylist = activities.list( |
| 76 | max_results='2', scope='@self', userId='@me').execute() |
| 77 | |
| 78 | # Add a comment to that activity |
| 79 | comment_body = { |
| 80 | "data": { |
| 81 | "content": "This is a comment" |
| 82 | } |
| 83 | } |
| 84 | item = activitylist['items'][0] |
| 85 | comment = p.comments().insert( |
| 86 | userId=item['actor']['id'], postId=item['id'], body=comment_body |
| 87 | ).execute() |
| 88 | print 'Added a comment to the new activity' |
| 89 | pprint.pprint(comment) |
| 90 | except CredentialsInvalidError: |
| 91 | print 'Your credentials are no longer valid.' |
| 92 | print 'Please re-run this application to re-authorize.' |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 93 | |
| 94 | if __name__ == '__main__': |
| 95 | main() |