Joe Gregorio | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 2 | # -*- coding: utf-8 -*- |
| 3 | # |
Joe Gregorio | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 4 | # Copyright (C) 2013 Google Inc. |
Joe Gregorio | 20a5aa9 | 2011-04-01 17:44:25 -0400 | [diff] [blame] | 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 | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 20 | Command-line application that retrieves the list of the user's posts.""" |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 21 | |
| 22 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 23 | |
Joe Gregorio | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 24 | import argparse |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 25 | import logging |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 26 | import os |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 27 | import sys |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 28 | |
Joe Gregorio | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 29 | import httplib2 |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 30 | |
Joe Gregorio | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 31 | from apiclient import discovery |
| 32 | from oauth2client import file |
| 33 | from oauth2client import client |
| 34 | from oauth2client import tools |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 35 | |
Joe Gregorio | 20a5aa9 | 2011-04-01 17:44:25 -0400 | [diff] [blame] | 36 | |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 37 | # CLIENT_SECRETS, name of a file containing the OAuth 2.0 information for this |
| 38 | # application, including client_id and client_secret, which are found |
| 39 | # on the API Access tab on the Google APIs |
Joe Gregorio | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 40 | # Console <http://code.google.com/apis/console>. |
| 41 | CLIENT_SECRETS = os.path.join(os.path.dirname(__file__), 'client_secrets.json') |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 42 | |
| 43 | # Set up a Flow object to be used if we need to authenticate. |
Joe Gregorio | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 44 | FLOW = client.flow_from_clientsecrets(CLIENT_SECRETS, |
Joe Gregorio | 323b300 | 2011-11-09 08:48:28 -0500 | [diff] [blame] | 45 | scope='https://www.googleapis.com/auth/plus.me', |
Joe Gregorio | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 46 | message=tools.message_if_missing(CLIENT_SECRETS)) |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 47 | |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 48 | |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 49 | def main(argv): |
Joe Gregorio | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 50 | # Parse command-line options. |
| 51 | parser = argparse.ArgumentParser( |
| 52 | description=__doc__, |
| 53 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 54 | parents=[tools.argparser]) |
| 55 | flags = parser.parse_args(argv[1:]) |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 56 | |
Joe Gregorio | 20a5aa9 | 2011-04-01 17:44:25 -0400 | [diff] [blame] | 57 | # If the Credentials don't exist or are invalid run through the native client |
| 58 | # flow. The Storage object will ensure that if successful the good |
| 59 | # Credentials will get written back to a file. |
Joe Gregorio | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 60 | storage = file.Storage('plus.dat') |
Joe Gregorio | fffa7d7 | 2011-02-18 17:20:39 -0500 | [diff] [blame] | 61 | credentials = storage.get() |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 62 | |
Joe Gregorio | 652898b | 2011-05-02 21:07:43 -0400 | [diff] [blame] | 63 | if credentials is None or credentials.invalid: |
Joe Gregorio | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 64 | credentials = tools.run(FLOW, storage, flags) |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 65 | |
Joe Gregorio | 20a5aa9 | 2011-04-01 17:44:25 -0400 | [diff] [blame] | 66 | # Create an httplib2.Http object to handle our HTTP requests and authorize it |
| 67 | # with our good Credentials. |
Joe Gregorio | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 68 | http = credentials.authorize(httplib2.Http()) |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 69 | |
Joe Gregorio | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 70 | service = discovery.build('plus', 'v1', http=http) |
Joe Gregorio | 652898b | 2011-05-02 21:07:43 -0400 | [diff] [blame] | 71 | |
Joe Gregorio | 7d79121 | 2011-05-16 21:58:52 -0700 | [diff] [blame] | 72 | try: |
Joe Gregorio | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 73 | person = service.people().get(userId='me').execute() |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 74 | |
Joe Gregorio | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 75 | print 'Got your ID: %s' % person['displayName'] |
Joe Gregorio | 323b300 | 2011-11-09 08:48:28 -0500 | [diff] [blame] | 76 | print |
Joe Gregorio | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 77 | print '%-040s -> %s' % ('[Activitity ID]', '[Content]') |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 78 | |
Joe Gregorio | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 79 | # Don't execute the request until we reach the paging loop below. |
Joe Gregorio | 323b300 | 2011-11-09 08:48:28 -0500 | [diff] [blame] | 80 | request = service.activities().list( |
| 81 | userId=person['id'], collection='public') |
Joe Gregorio | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 82 | |
Joe Gregorio | 323b300 | 2011-11-09 08:48:28 -0500 | [diff] [blame] | 83 | # Loop over every activity and print the ID and a short snippet of content. |
Joe Gregorio | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 84 | while request is not None: |
Joe Gregorio | 323b300 | 2011-11-09 08:48:28 -0500 | [diff] [blame] | 85 | activities_doc = request.execute() |
| 86 | for item in activities_doc.get('items', []): |
| 87 | print '%-040s -> %s' % (item['id'], item['object']['content'][:30]) |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 88 | |
Joe Gregorio | 323b300 | 2011-11-09 08:48:28 -0500 | [diff] [blame] | 89 | request = service.activities().list_next(request, activities_doc) |
Joe Gregorio | 652898b | 2011-05-02 21:07:43 -0400 | [diff] [blame] | 90 | |
Joe Gregorio | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 91 | except client.AccessTokenRefreshError: |
| 92 | print ('The credentials have been revoked or expired, please re-run' |
| 93 | 'the application to re-authorize.') |
Joe Gregorio | 652898b | 2011-05-02 21:07:43 -0400 | [diff] [blame] | 94 | |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 95 | if __name__ == '__main__': |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 96 | main(sys.argv) |