|
11 | 11 |
|
12 | 12 | __author__ = '[email protected] (Joe Gregorio)' |
13 | 13 |
|
14 | | -from apiclient.discovery import build |
15 | | -from apiclient.oauth import FlowThreeLegged |
16 | | -from apiclient.ext.authtools import run |
17 | | -from apiclient.ext.file import Storage |
18 | | -from apiclient.oauth import CredentialsInvalidError |
19 | | - |
| 14 | +import gflags |
20 | 15 | import httplib2 |
| 16 | +import logging |
21 | 17 | import pprint |
| 18 | +import sys |
| 19 | + |
| 20 | +from apiclient.discovery import build |
| 21 | +from oauth2client.file import Storage |
| 22 | +from oauth2client.client import OAuth2WebServerFlow |
| 23 | +from oauth2client.tools import run |
| 24 | + |
| 25 | +FLAGS = gflags.FLAGS |
| 26 | +FLOW = OAuth2WebServerFlow( |
| 27 | + client_id='433807057907.apps.googleusercontent.com', |
| 28 | + client_secret='jigtZpMApkRxncxikFpR+SFg', |
| 29 | + scope='https://www.googleapis.com/auth/buzz', |
| 30 | + user_agent='buzz-cmdline-sample/1.0') |
22 | 31 |
|
23 | | -# Uncomment the next line to get very detailed logging |
24 | | -#httplib2.debuglevel = 4 |
| 32 | +gflags.DEFINE_enum('logging_level', 'ERROR', |
| 33 | + ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], |
| 34 | + 'Set the level of logging detail.') |
25 | 35 |
|
26 | 36 |
|
27 | | -def main(): |
| 37 | +def main(argv): |
| 38 | + try: |
| 39 | + argv = FLAGS(argv) |
| 40 | + except gflags.FlagsError, e: |
| 41 | + print '%s\\nUsage: %s ARGS\\n%s' % (e, argv[0], FLAGS) |
| 42 | + sys.exit(1) |
| 43 | + |
| 44 | + logging.getLogger().setLevel(getattr(logging, FLAGS.logging_level)) |
| 45 | + |
28 | 46 | storage = Storage('buzz.dat') |
29 | 47 | credentials = storage.get() |
30 | 48 | if credentials is None or credentials.invalid == True: |
31 | | - buzz_discovery = build("buzz", "v1").auth_discovery() |
32 | | - |
33 | | - flow = FlowThreeLegged(buzz_discovery, |
34 | | - consumer_key='anonymous', |
35 | | - consumer_secret='anonymous', |
36 | | - user_agent='python-buzz-sample/1.0', |
37 | | - domain='anonymous', |
38 | | - scope='https://www.googleapis.com/auth/buzz', |
39 | | - xoauth_displayname='Google API Client Example App') |
40 | | - |
41 | | - credentials = run(flow, storage) |
| 49 | + credentials = run(FLOW, storage) |
42 | 50 |
|
43 | 51 | http = httplib2.Http() |
44 | 52 | http = credentials.authorize(http) |
45 | 53 |
|
| 54 | + # Build the Buzz service |
46 | 55 | service = build("buzz", "v1", http=http, |
47 | | - developerKey="AIzaSyDRRpR3GS1F1_jKNNM9HCNd2wJQyPG3oN0") |
| 56 | + developerKey="AIzaSyDRRpR3GS1F1_jKNNM9HCNd2wJQyPG3oN0") |
48 | 57 | activities = service.activities() |
49 | 58 |
|
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" |
55 | | - |
56 | | - # Retrieve the next two activities |
57 | | - if activitylist: |
58 | | - activitylist = activities.list_next(activitylist).execute() |
59 | | - print "Retrieved the next two activities" |
60 | | - |
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'} |
69 | | - } |
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 = service.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.' |
| 59 | + # Retrieve the first two activities |
| 60 | + activitylist = activities.list( |
| 61 | + max_results='2', scope='@self', userId='@me').execute() |
| 62 | + print "Retrieved the first two activities" |
| 63 | + |
| 64 | + # Retrieve the next two activities |
| 65 | + if activitylist: |
| 66 | + activitylist = activities.list_next(activitylist).execute() |
| 67 | + print "Retrieved the next two activities" |
| 68 | + |
| 69 | + # Add a new activity |
| 70 | + new_activity_body = { |
| 71 | + 'title': 'Testing insert', |
| 72 | + 'object': { |
| 73 | + 'content': |
| 74 | + u'Just a short note to show that insert is working. ☄', |
| 75 | + 'type': 'note'} |
| 76 | + } |
| 77 | + activity = activities.insert(userId='@me', body=new_activity_body).execute() |
| 78 | + print "Added a new activity" |
| 79 | + |
| 80 | + activitylist = activities.list( |
| 81 | + max_results='2', scope='@self', userId='@me').execute() |
| 82 | + |
| 83 | + # Add a comment to that activity |
| 84 | + comment_body = { |
| 85 | + "content": "This is a comment" |
| 86 | + } |
| 87 | + item = activitylist['items'][0] |
| 88 | + comment = service.comments().insert( |
| 89 | + userId=item['actor']['id'], postId=item['id'], body=comment_body |
| 90 | + ).execute() |
| 91 | + print 'Added a comment to the new activity' |
| 92 | + pprint.pprint(comment) |
93 | 93 |
|
94 | 94 | if __name__ == '__main__': |
95 | | - main() |
| 95 | + main(sys.argv) |
0 commit comments