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 | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 24 | import sys |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 25 | |
Joe Gregorio | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 26 | from oauth2client import client |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 27 | from googleapiclient import sample_tools |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 28 | |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 29 | |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 30 | def main(argv): |
Joe Gregorio | fff42f2 | 2013-04-05 12:53:46 -0400 | [diff] [blame] | 31 | # Authenticate and construct service. |
| 32 | service, flags = sample_tools.init( |
| 33 | argv, 'plus', 'v1', __doc__, __file__, |
| 34 | scope='https://www.googleapis.com/auth/plus.me') |
Joe Gregorio | 652898b | 2011-05-02 21:07:43 -0400 | [diff] [blame] | 35 | |
Joe Gregorio | 7d79121 | 2011-05-16 21:58:52 -0700 | [diff] [blame] | 36 | try: |
Joe Gregorio | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 37 | person = service.people().get(userId='me').execute() |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 38 | |
Joe Gregorio | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 39 | print 'Got your ID: %s' % person['displayName'] |
Joe Gregorio | 323b300 | 2011-11-09 08:48:28 -0500 | [diff] [blame] | 40 | print |
Joe Gregorio | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 41 | print '%-040s -> %s' % ('[Activitity ID]', '[Content]') |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 42 | |
Joe Gregorio | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 43 | # Don't execute the request until we reach the paging loop below. |
Joe Gregorio | 323b300 | 2011-11-09 08:48:28 -0500 | [diff] [blame] | 44 | request = service.activities().list( |
| 45 | userId=person['id'], collection='public') |
Joe Gregorio | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 46 | |
Joe Gregorio | 323b300 | 2011-11-09 08:48:28 -0500 | [diff] [blame] | 47 | # 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] | 48 | while request is not None: |
Joe Gregorio | 323b300 | 2011-11-09 08:48:28 -0500 | [diff] [blame] | 49 | activities_doc = request.execute() |
| 50 | for item in activities_doc.get('items', []): |
| 51 | print '%-040s -> %s' % (item['id'], item['object']['content'][:30]) |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 52 | |
Joe Gregorio | 323b300 | 2011-11-09 08:48:28 -0500 | [diff] [blame] | 53 | request = service.activities().list_next(request, activities_doc) |
Joe Gregorio | 652898b | 2011-05-02 21:07:43 -0400 | [diff] [blame] | 54 | |
Joe Gregorio | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 55 | except client.AccessTokenRefreshError: |
| 56 | print ('The credentials have been revoked or expired, please re-run' |
| 57 | 'the application to re-authorize.') |
Joe Gregorio | 652898b | 2011-05-02 21:07:43 -0400 | [diff] [blame] | 58 | |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 59 | if __name__ == '__main__': |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 60 | main(sys.argv) |