Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -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 | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 14 | import httplib2 |
| 15 | import pickle |
| 16 | import pprint |
| 17 | |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 18 | from apiclient.discovery import build |
| 19 | from oauth2client.file import Storage |
| 20 | from oauth2client.client import OAuth2WebServerFlow |
| 21 | from oauth2client.tools import run |
| 22 | |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 23 | # Uncomment the next line to get very detailed logging |
| 24 | #httplib2.debuglevel = 4 |
| 25 | |
| 26 | |
| 27 | def main(): |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 28 | storage = Storage('buzz.dat') |
| 29 | credentials = storage.get() |
Joe Gregorio | 9ce4b62 | 2011-02-17 15:32:11 -0500 | [diff] [blame] | 30 | if credentials is None or credentials.invalid == True: |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 31 | flow = OAuth2WebServerFlow( |
| 32 | client_id='433807057907.apps.googleusercontent.com', |
| 33 | client_secret='jigtZpMApkRxncxikFpR+SFg', |
| 34 | scope='https://www.googleapis.com/auth/buzz', |
| 35 | user_agent='buzz-cmdline-sample/1.0', |
| 36 | domain='anonymous', |
| 37 | xoauth_displayname='Buzz Client Example App' |
| 38 | ) |
| 39 | credentials = run(flow, storage) |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 40 | |
| 41 | http = httplib2.Http() |
| 42 | http = credentials.authorize(http) |
| 43 | |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 44 | p = build("buzz", "v1", http=http, |
| 45 | developerKey="AIzaSyDRRpR3GS1F1_jKNNM9HCNd2wJQyPG3oN0") |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 46 | activities = p.activities() |
| 47 | |
| 48 | # Retrieve the first two activities |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 49 | activitylist = activities.list( |
| 50 | max_results='2', scope='@self', userId='@me').execute() |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 51 | print "Retrieved the first two activities" |
| 52 | |
| 53 | # Retrieve the next two activities |
| 54 | if activitylist: |
| 55 | activitylist = activities.list_next(activitylist).execute() |
| 56 | print "Retrieved the next two activities" |
| 57 | |
| 58 | # Add a new activity |
| 59 | new_activity_body = { |
| 60 | "data": { |
| 61 | 'title': 'Testing insert', |
| 62 | 'object': { |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 63 | 'content': |
| 64 | u'Just a short note to show that insert is working. ☄', |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 65 | 'type': 'note'} |
| 66 | } |
| 67 | } |
| 68 | activity = activities.insert(userId='@me', body=new_activity_body).execute() |
| 69 | print "Added a new activity" |
| 70 | |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 71 | activitylist = activities.list( |
| 72 | max_results='2', scope='@self', userId='@me').execute() |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 73 | |
| 74 | # Add a comment to that activity |
| 75 | comment_body = { |
| 76 | "data": { |
| 77 | "content": "This is a comment" |
| 78 | } |
| 79 | } |
| 80 | item = activitylist['items'][0] |
| 81 | comment = p.comments().insert( |
| 82 | userId=item['actor']['id'], postId=item['id'], body=comment_body |
| 83 | ).execute() |
| 84 | print 'Added a comment to the new activity' |
| 85 | pprint.pprint(comment) |
| 86 | |
| 87 | if __name__ == '__main__': |
| 88 | main() |