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 | 323b300 | 2011-11-09 08:48:28 -0500 | [diff] [blame] | 18 | """Simple command-line sample for the Google+ API. |
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 | 323b300 | 2011-11-09 08:48:28 -0500 | [diff] [blame] | 24 | $ python plus.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 | 323b300 | 2011-11-09 08:48:28 -0500 | [diff] [blame] | 29 | $ python plus.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 | 323b300 | 2011-11-09 08:48:28 -0500 | [diff] [blame] | 33 | $ python plus.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 |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 41 | import os |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 42 | import pprint |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 43 | import sys |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 44 | |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 45 | from apiclient.discovery import build |
| 46 | from oauth2client.file import Storage |
Joe Gregorio | 7d79121 | 2011-05-16 21:58:52 -0700 | [diff] [blame] | 47 | from oauth2client.client import AccessTokenRefreshError |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 48 | from oauth2client.client import flow_from_clientsecrets |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 49 | from oauth2client.tools import run |
| 50 | |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 51 | |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 52 | FLAGS = gflags.FLAGS |
Joe Gregorio | 20a5aa9 | 2011-04-01 17:44:25 -0400 | [diff] [blame] | 53 | |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 54 | # CLIENT_SECRETS, name of a file containing the OAuth 2.0 information for this |
| 55 | # application, including client_id and client_secret, which are found |
| 56 | # on the API Access tab on the Google APIs |
| 57 | # Console <http://code.google.com/apis/console> |
| 58 | CLIENT_SECRETS = 'client_secrets.json' |
| 59 | |
| 60 | # Helpful message to display in the browser if the CLIENT_SECRETS file |
| 61 | # is missing. |
| 62 | MISSING_CLIENT_SECRETS_MESSAGE = """ |
| 63 | WARNING: Please configure OAuth 2.0 |
| 64 | |
| 65 | To make this sample run you will need to populate the client_secrets.json file |
| 66 | found at: |
| 67 | |
| 68 | %s |
| 69 | |
| 70 | with information from the APIs Console <https://code.google.com/apis/console>. |
| 71 | |
| 72 | """ % os.path.join(os.path.dirname(__file__), CLIENT_SECRETS) |
| 73 | |
| 74 | # Set up a Flow object to be used if we need to authenticate. |
| 75 | FLOW = flow_from_clientsecrets(CLIENT_SECRETS, |
Joe Gregorio | 323b300 | 2011-11-09 08:48:28 -0500 | [diff] [blame] | 76 | scope='https://www.googleapis.com/auth/plus.me', |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 77 | message=MISSING_CLIENT_SECRETS_MESSAGE) |
| 78 | |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 79 | |
Joe Gregorio | 20a5aa9 | 2011-04-01 17:44:25 -0400 | [diff] [blame] | 80 | # The gflags module makes defining command-line options easy for |
| 81 | # applications. Run this program with the '--help' argument to see |
| 82 | # all the flags that it understands. |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 83 | gflags.DEFINE_enum('logging_level', 'ERROR', |
| 84 | ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], |
| 85 | 'Set the level of logging detail.') |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 86 | |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 87 | |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 88 | def main(argv): |
Joe Gregorio | 20a5aa9 | 2011-04-01 17:44:25 -0400 | [diff] [blame] | 89 | # Let the gflags module process the command-line arguments |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 90 | try: |
| 91 | argv = FLAGS(argv) |
| 92 | except gflags.FlagsError, e: |
| 93 | print '%s\\nUsage: %s ARGS\\n%s' % (e, argv[0], FLAGS) |
| 94 | sys.exit(1) |
| 95 | |
Joe Gregorio | 20a5aa9 | 2011-04-01 17:44:25 -0400 | [diff] [blame] | 96 | # Set the logging according to the command-line flag |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 97 | logging.getLogger().setLevel(getattr(logging, FLAGS.logging_level)) |
| 98 | |
Joe Gregorio | 20a5aa9 | 2011-04-01 17:44:25 -0400 | [diff] [blame] | 99 | # If the Credentials don't exist or are invalid run through the native client |
| 100 | # flow. The Storage object will ensure that if successful the good |
| 101 | # Credentials will get written back to a file. |
Joe Gregorio | 323b300 | 2011-11-09 08:48:28 -0500 | [diff] [blame] | 102 | storage = Storage('plus.dat') |
Joe Gregorio | fffa7d7 | 2011-02-18 17:20:39 -0500 | [diff] [blame] | 103 | credentials = storage.get() |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 104 | |
Joe Gregorio | 652898b | 2011-05-02 21:07:43 -0400 | [diff] [blame] | 105 | if credentials is None or credentials.invalid: |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 106 | credentials = run(FLOW, storage) |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 107 | |
Joe Gregorio | 20a5aa9 | 2011-04-01 17:44:25 -0400 | [diff] [blame] | 108 | # Create an httplib2.Http object to handle our HTTP requests and authorize it |
| 109 | # with our good Credentials. |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 110 | http = httplib2.Http() |
| 111 | http = credentials.authorize(http) |
| 112 | |
Joe Gregorio | 323b300 | 2011-11-09 08:48:28 -0500 | [diff] [blame] | 113 | service = build("plus", "v1", http=http) |
Joe Gregorio | 652898b | 2011-05-02 21:07:43 -0400 | [diff] [blame] | 114 | |
Joe Gregorio | 7d79121 | 2011-05-16 21:58:52 -0700 | [diff] [blame] | 115 | try: |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame^] | 116 | person = service.people().get(userId='me').execute(http=http) |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 117 | |
Joe Gregorio | 323b300 | 2011-11-09 08:48:28 -0500 | [diff] [blame] | 118 | print "Got your ID: %s" % person['displayName'] |
| 119 | print |
| 120 | print "%-040s -> %s" % ("[Activitity ID]", "[Content]") |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 121 | |
Joe Gregorio | 323b300 | 2011-11-09 08:48:28 -0500 | [diff] [blame] | 122 | # Don't execute the request until we reach the paging loop below |
| 123 | request = service.activities().list( |
| 124 | userId=person['id'], collection='public') |
| 125 | # Loop over every activity and print the ID and a short snippet of content. |
| 126 | while ( request != None ): |
| 127 | activities_doc = request.execute() |
| 128 | for item in activities_doc.get('items', []): |
| 129 | print '%-040s -> %s' % (item['id'], item['object']['content'][:30]) |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 130 | |
Joe Gregorio | 323b300 | 2011-11-09 08:48:28 -0500 | [diff] [blame] | 131 | request = service.activities().list_next(request, activities_doc) |
Joe Gregorio | 652898b | 2011-05-02 21:07:43 -0400 | [diff] [blame] | 132 | |
Joe Gregorio | 7d79121 | 2011-05-16 21:58:52 -0700 | [diff] [blame] | 133 | except AccessTokenRefreshError: |
| 134 | print ("The credentials have been revoked or expired, please re-run" |
| 135 | "the application to re-authorize") |
Joe Gregorio | 652898b | 2011-05-02 21:07:43 -0400 | [diff] [blame] | 136 | |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 137 | if __name__ == '__main__': |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 138 | main(sys.argv) |