blob: b88395b25dfe0afc540a1b55811713937b6458b9 [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 Gregorio79daca02013-03-29 16:25:52 -040024import argparse
Joe Gregorio6abf8702011-03-18 22:44:17 -040025import logging
Joe Gregoriof08a4982011-10-07 13:11:16 -040026import os
Joe Gregorio6abf8702011-03-18 22:44:17 -040027import sys
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040028
Joe Gregorio79daca02013-03-29 16:25:52 -040029import httplib2
Joe Gregorio6abf8702011-03-18 22:44:17 -040030
Joe Gregorio79daca02013-03-29 16:25:52 -040031from apiclient import discovery
32from oauth2client import file
33from oauth2client import client
34from oauth2client import tools
Joe Gregoriof08a4982011-10-07 13:11:16 -040035
Joe Gregorio20a5aa92011-04-01 17:44:25 -040036
Joe Gregoriof08a4982011-10-07 13:11:16 -040037# 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 Gregorio79daca02013-03-29 16:25:52 -040040# Console <http://code.google.com/apis/console>.
41CLIENT_SECRETS = os.path.join(os.path.dirname(__file__), 'client_secrets.json')
Joe Gregoriof08a4982011-10-07 13:11:16 -040042
43# Set up a Flow object to be used if we need to authenticate.
Joe Gregorio79daca02013-03-29 16:25:52 -040044FLOW = client.flow_from_clientsecrets(CLIENT_SECRETS,
Joe Gregorio323b3002011-11-09 08:48:28 -050045 scope='https://www.googleapis.com/auth/plus.me',
Joe Gregorio79daca02013-03-29 16:25:52 -040046 message=tools.message_if_missing(CLIENT_SECRETS))
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040047
Joe Gregorioaf276d22010-12-09 14:26:58 -050048
Joe Gregorio6abf8702011-03-18 22:44:17 -040049def main(argv):
Joe Gregorio79daca02013-03-29 16:25:52 -040050 # 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 Gregorio6abf8702011-03-18 22:44:17 -040056
Joe Gregorio20a5aa92011-04-01 17:44:25 -040057 # 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 Gregorio79daca02013-03-29 16:25:52 -040060 storage = file.Storage('plus.dat')
Joe Gregoriofffa7d72011-02-18 17:20:39 -050061 credentials = storage.get()
Joe Gregorio562b7312011-09-15 09:06:38 -040062
Joe Gregorio652898b2011-05-02 21:07:43 -040063 if credentials is None or credentials.invalid:
Joe Gregorio79daca02013-03-29 16:25:52 -040064 credentials = tools.run(FLOW, storage, flags)
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040065
Joe Gregorio20a5aa92011-04-01 17:44:25 -040066 # Create an httplib2.Http object to handle our HTTP requests and authorize it
67 # with our good Credentials.
Joe Gregorio79daca02013-03-29 16:25:52 -040068 http = credentials.authorize(httplib2.Http())
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040069
Joe Gregorio79daca02013-03-29 16:25:52 -040070 service = discovery.build('plus', 'v1', http=http)
Joe Gregorio652898b2011-05-02 21:07:43 -040071
Joe Gregorio7d791212011-05-16 21:58:52 -070072 try:
Joe Gregorio79daca02013-03-29 16:25:52 -040073 person = service.people().get(userId='me').execute()
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040074
Joe Gregorio79daca02013-03-29 16:25:52 -040075 print 'Got your ID: %s' % person['displayName']
Joe Gregorio323b3002011-11-09 08:48:28 -050076 print
Joe Gregorio79daca02013-03-29 16:25:52 -040077 print '%-040s -> %s' % ('[Activitity ID]', '[Content]')
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040078
Joe Gregorio79daca02013-03-29 16:25:52 -040079 # Don't execute the request until we reach the paging loop below.
Joe Gregorio323b3002011-11-09 08:48:28 -050080 request = service.activities().list(
81 userId=person['id'], collection='public')
Joe Gregorio79daca02013-03-29 16:25:52 -040082
Joe Gregorio323b3002011-11-09 08:48:28 -050083 # Loop over every activity and print the ID and a short snippet of content.
Joe Gregorio79daca02013-03-29 16:25:52 -040084 while request is not None:
Joe Gregorio323b3002011-11-09 08:48:28 -050085 activities_doc = request.execute()
86 for item in activities_doc.get('items', []):
87 print '%-040s -> %s' % (item['id'], item['object']['content'][:30])
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040088
Joe Gregorio323b3002011-11-09 08:48:28 -050089 request = service.activities().list_next(request, activities_doc)
Joe Gregorio652898b2011-05-02 21:07:43 -040090
Joe Gregorio79daca02013-03-29 16:25:52 -040091 except client.AccessTokenRefreshError:
92 print ('The credentials have been revoked or expired, please re-run'
93 'the application to re-authorize.')
Joe Gregorio652898b2011-05-02 21:07:43 -040094
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040095if __name__ == '__main__':
Joe Gregorio6abf8702011-03-18 22:44:17 -040096 main(sys.argv)