blob: cc7d146ed87340cf2928cdd8340ba01445713e87 [file] [log] [blame]
Joe Gregorio79daca02013-03-29 16:25:52 -04001#!/usr/bin/env python
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -04002# -*- coding: utf-8 -*-
3#
Craig Citro751b7fb2014-09-23 11:20:38 -07004# Copyright 2014 Google Inc. All Rights Reserved.
Joe Gregorio20a5aa92011-04-01 17:44:25 -04005#
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.come3c8b6d2010-10-07 19:34:54 -040017
Joe Gregorio323b3002011-11-09 08:48:28 -050018"""Simple command-line sample for the Google+ API.
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040019
Joe Gregorio79daca02013-03-29 16:25:52 -040020Command-line application that retrieves the list of the user's posts."""
INADA Naokie8d87822014-08-20 15:25:24 +090021from __future__ import print_function
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040022
23__author__ = 'jcgregorio@google.com (Joe Gregorio)'
24
Joe Gregorio6abf8702011-03-18 22:44:17 -040025import sys
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040026
Joe Gregorio79daca02013-03-29 16:25:52 -040027from oauth2client import client
John Asmuth864311d2014-04-24 15:46:08 -040028from googleapiclient import sample_tools
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040029
Joe Gregorioaf276d22010-12-09 14:26:58 -050030
Joe Gregorio6abf8702011-03-18 22:44:17 -040031def main(argv):
Joe Gregoriofff42f22013-04-05 12:53:46 -040032 # Authenticate and construct service.
33 service, flags = sample_tools.init(
34 argv, 'plus', 'v1', __doc__, __file__,
35 scope='https://www.googleapis.com/auth/plus.me')
Joe Gregorio652898b2011-05-02 21:07:43 -040036
Joe Gregorio7d791212011-05-16 21:58:52 -070037 try:
Joe Gregorio79daca02013-03-29 16:25:52 -040038 person = service.people().get(userId='me').execute()
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040039
INADA Naokie8d87822014-08-20 15:25:24 +090040 print('Got your ID: %s' % person['displayName'])
41 print()
42 print('%-040s -> %s' % ('[Activitity ID]', '[Content]'))
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040043
Joe Gregorio79daca02013-03-29 16:25:52 -040044 # Don't execute the request until we reach the paging loop below.
Joe Gregorio323b3002011-11-09 08:48:28 -050045 request = service.activities().list(
46 userId=person['id'], collection='public')
Joe Gregorio79daca02013-03-29 16:25:52 -040047
Joe Gregorio323b3002011-11-09 08:48:28 -050048 # Loop over every activity and print the ID and a short snippet of content.
Joe Gregorio79daca02013-03-29 16:25:52 -040049 while request is not None:
Joe Gregorio323b3002011-11-09 08:48:28 -050050 activities_doc = request.execute()
51 for item in activities_doc.get('items', []):
INADA Naokie8d87822014-08-20 15:25:24 +090052 print('%-040s -> %s' % (item['id'], item['object']['content'][:30]))
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040053
Joe Gregorio323b3002011-11-09 08:48:28 -050054 request = service.activities().list_next(request, activities_doc)
Joe Gregorio652898b2011-05-02 21:07:43 -040055
Joe Gregorio79daca02013-03-29 16:25:52 -040056 except client.AccessTokenRefreshError:
57 print ('The credentials have been revoked or expired, please re-run'
58 'the application to re-authorize.')
Joe Gregorio652898b2011-05-02 21:07:43 -040059
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040060if __name__ == '__main__':
Joe Gregorio6abf8702011-03-18 22:44:17 -040061 main(sys.argv)