jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 1 | #!/usr/bin/python2.4 |
| 2 | # -*- coding: utf-8 -*- |
| 3 | # |
Joe Gregorio | 20a5aa9 | 2011-04-01 17:44:25 -0400 | [diff] [blame^] | 4 | # Copyright (C) 2010 Google Inc. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 17 | |
| 18 | """Simple command-line example for Buzz. |
| 19 | |
Joe Gregorio | 20a5aa9 | 2011-04-01 17:44:25 -0400 | [diff] [blame^] | 20 | Command-line application that retrieves the users latest content and then adds |
| 21 | a new entry. |
| 22 | |
| 23 | Usage: |
| 24 | $ python buzz.py.py |
| 25 | |
| 26 | You can also get help on all the command-line flags the program understands |
| 27 | by running: |
| 28 | |
| 29 | $ python buzz.py.py --help |
| 30 | |
| 31 | To get detailed log output run: |
| 32 | |
| 33 | $ python buzz.py.py --logging_level=DEBUG |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 34 | """ |
| 35 | |
| 36 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 37 | |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 38 | import gflags |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 39 | import httplib2 |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 40 | import logging |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 41 | import pprint |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 42 | import sys |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 43 | |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 44 | from apiclient.discovery import build |
| 45 | from oauth2client.file import Storage |
| 46 | from oauth2client.client import OAuth2WebServerFlow |
| 47 | from oauth2client.tools import run |
| 48 | |
| 49 | FLAGS = gflags.FLAGS |
Joe Gregorio | 20a5aa9 | 2011-04-01 17:44:25 -0400 | [diff] [blame^] | 50 | |
| 51 | # Set up a Flow object to be used if we need to authenticate. This |
| 52 | # sample uses OAuth 2.0, and we set up the OAuth2WebServerFlow with |
| 53 | # the information it needs to authenticate. Note that it is called |
| 54 | # the Web Server Flow, but it can also handle the flow for native |
| 55 | # applications <http://code.google.com/apis/accounts/docs/OAuth2.html#IA> |
| 56 | # The client_id client_secret are copied from the Identity tab on |
| 57 | # the Google APIs Console <http://code.google.com/apis/console> |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 58 | FLOW = OAuth2WebServerFlow( |
| 59 | client_id='433807057907.apps.googleusercontent.com', |
| 60 | client_secret='jigtZpMApkRxncxikFpR+SFg', |
| 61 | scope='https://www.googleapis.com/auth/buzz', |
| 62 | user_agent='buzz-cmdline-sample/1.0') |
| 63 | |
Joe Gregorio | 20a5aa9 | 2011-04-01 17:44:25 -0400 | [diff] [blame^] | 64 | # The gflags module makes defining command-line options easy for |
| 65 | # applications. Run this program with the '--help' argument to see |
| 66 | # all the flags that it understands. |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 67 | gflags.DEFINE_enum('logging_level', 'ERROR', |
| 68 | ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], |
| 69 | 'Set the level of logging detail.') |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 70 | |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 71 | |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 72 | def main(argv): |
Joe Gregorio | 20a5aa9 | 2011-04-01 17:44:25 -0400 | [diff] [blame^] | 73 | # Let the gflags module process the command-line arguments |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 74 | try: |
| 75 | argv = FLAGS(argv) |
| 76 | except gflags.FlagsError, e: |
| 77 | print '%s\\nUsage: %s ARGS\\n%s' % (e, argv[0], FLAGS) |
| 78 | sys.exit(1) |
| 79 | |
Joe Gregorio | 20a5aa9 | 2011-04-01 17:44:25 -0400 | [diff] [blame^] | 80 | # Set the logging according to the command-line flag |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 81 | logging.getLogger().setLevel(getattr(logging, FLAGS.logging_level)) |
| 82 | |
Joe Gregorio | 20a5aa9 | 2011-04-01 17:44:25 -0400 | [diff] [blame^] | 83 | # If the Credentials don't exist or are invalid run through the native client |
| 84 | # flow. The Storage object will ensure that if successful the good |
| 85 | # Credentials will get written back to a file. |
Joe Gregorio | fffa7d7 | 2011-02-18 17:20:39 -0500 | [diff] [blame] | 86 | storage = Storage('buzz.dat') |
| 87 | credentials = storage.get() |
Joe Gregorio | a0a52e4 | 2011-02-17 17:13:26 -0500 | [diff] [blame] | 88 | if credentials is None or credentials.invalid == True: |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 89 | credentials = run(FLOW, storage) |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 90 | |
Joe Gregorio | 20a5aa9 | 2011-04-01 17:44:25 -0400 | [diff] [blame^] | 91 | # Create an httplib2.Http object to handle our HTTP requests and authorize it |
| 92 | # with our good Credentials. |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 93 | http = httplib2.Http() |
| 94 | http = credentials.authorize(http) |
| 95 | |
Joe Gregorio | 20a5aa9 | 2011-04-01 17:44:25 -0400 | [diff] [blame^] | 96 | # Build a service object for interacting with the API. Visit |
| 97 | # the Google APIs Console <http://code.google.com/apis/console> |
| 98 | # to get a developerKey for your own application. |
Joe Gregorio | 1ae3e74 | 2011-02-25 15:17:14 -0500 | [diff] [blame] | 99 | service = build("buzz", "v1", http=http, |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 100 | developerKey="AIzaSyDRRpR3GS1F1_jKNNM9HCNd2wJQyPG3oN0") |
Joe Gregorio | 1ae3e74 | 2011-02-25 15:17:14 -0500 | [diff] [blame] | 101 | activities = service.activities() |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 102 | |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 103 | # Retrieve the first two activities |
| 104 | activitylist = activities.list( |
| 105 | max_results='2', scope='@self', userId='@me').execute() |
| 106 | print "Retrieved the first two activities" |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 107 | |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 108 | # Retrieve the next two activities |
| 109 | if activitylist: |
| 110 | activitylist = activities.list_next(activitylist).execute() |
| 111 | print "Retrieved the next two activities" |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 112 | |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 113 | # Add a new activity |
| 114 | new_activity_body = { |
| 115 | 'title': 'Testing insert', |
| 116 | 'object': { |
| 117 | 'content': |
| 118 | u'Just a short note to show that insert is working. ☄', |
| 119 | 'type': 'note'} |
| 120 | } |
| 121 | activity = activities.insert(userId='@me', body=new_activity_body).execute() |
| 122 | print "Added a new activity" |
Joe Gregorio | a0a52e4 | 2011-02-17 17:13:26 -0500 | [diff] [blame] | 123 | |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 124 | activitylist = activities.list( |
| 125 | max_results='2', scope='@self', userId='@me').execute() |
Joe Gregorio | a0a52e4 | 2011-02-17 17:13:26 -0500 | [diff] [blame] | 126 | |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 127 | # Add a comment to that activity |
| 128 | comment_body = { |
| 129 | "content": "This is a comment" |
| 130 | } |
| 131 | item = activitylist['items'][0] |
| 132 | comment = service.comments().insert( |
| 133 | userId=item['actor']['id'], postId=item['id'], body=comment_body |
| 134 | ).execute() |
| 135 | print 'Added a comment to the new activity' |
| 136 | pprint.pprint(comment) |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 137 | |
| 138 | if __name__ == '__main__': |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 139 | main(sys.argv) |