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 | |
Joe Gregorio | 652898b | 2011-05-02 21:07:43 -0400 | [diff] [blame] | 18 | """Simple command-line sample for Buzz. |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 19 | |
Joe Gregorio | 652898b | 2011-05-02 21:07:43 -0400 | [diff] [blame] | 20 | Command-line application that retrieves the users latest content and |
| 21 | then adds a new entry. |
Joe Gregorio | 20a5aa9 | 2011-04-01 17:44:25 -0400 | [diff] [blame] | 22 | |
| 23 | Usage: |
Joe Gregorio | 652898b | 2011-05-02 21:07:43 -0400 | [diff] [blame] | 24 | $ python buzz.py |
Joe Gregorio | 20a5aa9 | 2011-04-01 17:44:25 -0400 | [diff] [blame] | 25 | |
| 26 | You can also get help on all the command-line flags the program understands |
| 27 | by running: |
| 28 | |
Joe Gregorio | 652898b | 2011-05-02 21:07:43 -0400 | [diff] [blame] | 29 | $ python buzz.py --help |
Joe Gregorio | 20a5aa9 | 2011-04-01 17:44:25 -0400 | [diff] [blame] | 30 | |
| 31 | To get detailed log output run: |
| 32 | |
Joe Gregorio | 652898b | 2011-05-02 21:07:43 -0400 | [diff] [blame] | 33 | $ python buzz.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 |
Joe Gregorio | 7d79121 | 2011-05-16 21:58:52 -0700 | [diff] [blame] | 46 | from oauth2client.client import AccessTokenRefreshError |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 47 | from oauth2client.client import OAuth2WebServerFlow |
| 48 | from oauth2client.tools import run |
| 49 | |
| 50 | FLAGS = gflags.FLAGS |
Joe Gregorio | 20a5aa9 | 2011-04-01 17:44:25 -0400 | [diff] [blame] | 51 | |
| 52 | # Set up a Flow object to be used if we need to authenticate. This |
| 53 | # sample uses OAuth 2.0, and we set up the OAuth2WebServerFlow with |
| 54 | # the information it needs to authenticate. Note that it is called |
| 55 | # the Web Server Flow, but it can also handle the flow for native |
| 56 | # applications <http://code.google.com/apis/accounts/docs/OAuth2.html#IA> |
Joe Gregorio | 652898b | 2011-05-02 21:07:43 -0400 | [diff] [blame] | 57 | # The client_id client_secret are copied from the API Access tab on |
| 58 | # the Google APIs Console <http://code.google.com/apis/console>. When |
| 59 | # creating credentials for this application be sure to choose an Application |
| 60 | # type of "Installed application". |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 61 | FLOW = OAuth2WebServerFlow( |
| 62 | client_id='433807057907.apps.googleusercontent.com', |
| 63 | client_secret='jigtZpMApkRxncxikFpR+SFg', |
| 64 | scope='https://www.googleapis.com/auth/buzz', |
| 65 | user_agent='buzz-cmdline-sample/1.0') |
| 66 | |
Joe Gregorio | 20a5aa9 | 2011-04-01 17:44:25 -0400 | [diff] [blame] | 67 | # The gflags module makes defining command-line options easy for |
| 68 | # applications. Run this program with the '--help' argument to see |
| 69 | # all the flags that it understands. |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 70 | gflags.DEFINE_enum('logging_level', 'ERROR', |
| 71 | ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], |
| 72 | 'Set the level of logging detail.') |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 73 | |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 74 | |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 75 | def main(argv): |
Joe Gregorio | 20a5aa9 | 2011-04-01 17:44:25 -0400 | [diff] [blame] | 76 | # Let the gflags module process the command-line arguments |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 77 | try: |
| 78 | argv = FLAGS(argv) |
| 79 | except gflags.FlagsError, e: |
| 80 | print '%s\\nUsage: %s ARGS\\n%s' % (e, argv[0], FLAGS) |
| 81 | sys.exit(1) |
| 82 | |
Joe Gregorio | 20a5aa9 | 2011-04-01 17:44:25 -0400 | [diff] [blame] | 83 | # Set the logging according to the command-line flag |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 84 | logging.getLogger().setLevel(getattr(logging, FLAGS.logging_level)) |
| 85 | |
Joe Gregorio | 20a5aa9 | 2011-04-01 17:44:25 -0400 | [diff] [blame] | 86 | # If the Credentials don't exist or are invalid run through the native client |
| 87 | # flow. The Storage object will ensure that if successful the good |
| 88 | # Credentials will get written back to a file. |
Joe Gregorio | fffa7d7 | 2011-02-18 17:20:39 -0500 | [diff] [blame] | 89 | storage = Storage('buzz.dat') |
| 90 | credentials = storage.get() |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 91 | |
Joe Gregorio | 652898b | 2011-05-02 21:07:43 -0400 | [diff] [blame] | 92 | if credentials is None or credentials.invalid: |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 93 | credentials = run(FLOW, storage) |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 94 | |
Joe Gregorio | 20a5aa9 | 2011-04-01 17:44:25 -0400 | [diff] [blame] | 95 | # Create an httplib2.Http object to handle our HTTP requests and authorize it |
| 96 | # with our good Credentials. |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 97 | http = httplib2.Http() |
| 98 | http = credentials.authorize(http) |
| 99 | |
Joe Gregorio | 652898b | 2011-05-02 21:07:43 -0400 | [diff] [blame] | 100 | service = build("buzz", "v1", http=http) |
| 101 | |
Joe Gregorio | 7d79121 | 2011-05-16 21:58:52 -0700 | [diff] [blame] | 102 | try: |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 103 | |
Joe Gregorio | 7d79121 | 2011-05-16 21:58:52 -0700 | [diff] [blame] | 104 | activities = service.activities() |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 105 | |
Joe Gregorio | 7d79121 | 2011-05-16 21:58:52 -0700 | [diff] [blame] | 106 | # Retrieve the first two activities |
| 107 | activitylist = activities.list( |
| 108 | max_results='2', scope='@self', userId='@me').execute() |
| 109 | print "Retrieved the first two activities" |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 110 | |
Joe Gregorio | 7d79121 | 2011-05-16 21:58:52 -0700 | [diff] [blame] | 111 | # Retrieve the next two activities |
| 112 | if activitylist: |
| 113 | activitylist = activities.list_next(activitylist).execute() |
| 114 | print "Retrieved the next two activities" |
Joe Gregorio | a0a52e4 | 2011-02-17 17:13:26 -0500 | [diff] [blame] | 115 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 116 | # List the number of followers |
| 117 | followers = service.people().list( |
| 118 | userId='@me', groupId='@followers').execute(http) |
| 119 | print 'Hello, you have %s followers!' % followers['totalResults'] |
Joe Gregorio | 652898b | 2011-05-02 21:07:43 -0400 | [diff] [blame] | 120 | |
Joe Gregorio | 7d79121 | 2011-05-16 21:58:52 -0700 | [diff] [blame] | 121 | except AccessTokenRefreshError: |
| 122 | print ("The credentials have been revoked or expired, please re-run" |
| 123 | "the application to re-authorize") |
Joe Gregorio | 652898b | 2011-05-02 21:07:43 -0400 | [diff] [blame] | 124 | |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 125 | if __name__ == '__main__': |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 126 | main(sys.argv) |