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