blob: b9dde0c8f407ef38bef454fb78c28ef6b868af67 [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#
Joe Gregorio79daca02013-03-29 16:25:52 -04004# Copyright (C) 2013 Google Inc.
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."""
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040021
22__author__ = 'jcgregorio@google.com (Joe Gregorio)'
23
Joe Gregorio6abf8702011-03-18 22:44:17 -040024import sys
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040025
Joe Gregorio79daca02013-03-29 16:25:52 -040026from oauth2client import client
John Asmuth864311d2014-04-24 15:46:08 -040027from googleapiclient import sample_tools
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040028
Joe Gregorioaf276d22010-12-09 14:26:58 -050029
Joe Gregorio6abf8702011-03-18 22:44:17 -040030def main(argv):
Joe Gregoriofff42f22013-04-05 12:53:46 -040031 # 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 Gregorio652898b2011-05-02 21:07:43 -040035
Joe Gregorio7d791212011-05-16 21:58:52 -070036 try:
Joe Gregorio79daca02013-03-29 16:25:52 -040037 person = service.people().get(userId='me').execute()
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040038
Joe Gregorio79daca02013-03-29 16:25:52 -040039 print 'Got your ID: %s' % person['displayName']
Joe Gregorio323b3002011-11-09 08:48:28 -050040 print
Joe Gregorio79daca02013-03-29 16:25:52 -040041 print '%-040s -> %s' % ('[Activitity ID]', '[Content]')
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040042
Joe Gregorio79daca02013-03-29 16:25:52 -040043 # Don't execute the request until we reach the paging loop below.
Joe Gregorio323b3002011-11-09 08:48:28 -050044 request = service.activities().list(
45 userId=person['id'], collection='public')
Joe Gregorio79daca02013-03-29 16:25:52 -040046
Joe Gregorio323b3002011-11-09 08:48:28 -050047 # Loop over every activity and print the ID and a short snippet of content.
Joe Gregorio79daca02013-03-29 16:25:52 -040048 while request is not None:
Joe Gregorio323b3002011-11-09 08:48:28 -050049 activities_doc = request.execute()
50 for item in activities_doc.get('items', []):
51 print '%-040s -> %s' % (item['id'], item['object']['content'][:30])
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040052
Joe Gregorio323b3002011-11-09 08:48:28 -050053 request = service.activities().list_next(request, activities_doc)
Joe Gregorio652898b2011-05-02 21:07:43 -040054
Joe Gregorio79daca02013-03-29 16:25:52 -040055 except client.AccessTokenRefreshError:
56 print ('The credentials have been revoked or expired, please re-run'
57 'the application to re-authorize.')
Joe Gregorio652898b2011-05-02 21:07:43 -040058
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040059if __name__ == '__main__':
Joe Gregorio6abf8702011-03-18 22:44:17 -040060 main(sys.argv)