blob: 6334ed66918992ea4f3e62c7a2052c40fd93124a [file] [log] [blame]
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -04001#!/usr/bin/python2.4
2# -*- coding: utf-8 -*-
3#
Joe Gregorio20a5aa92011-04-01 17:44:25 -04004# Copyright (C) 2010 Google Inc.
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.come3c8b6d2010-10-07 19:34:54 -040017
Joe Gregorio652898b2011-05-02 21:07:43 -040018"""Simple command-line sample for Buzz.
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040019
Joe Gregorio652898b2011-05-02 21:07:43 -040020Command-line application that retrieves the users latest content and
21then adds a new entry.
Joe Gregorio20a5aa92011-04-01 17:44:25 -040022
23Usage:
Joe Gregorio652898b2011-05-02 21:07:43 -040024 $ python buzz.py
Joe Gregorio20a5aa92011-04-01 17:44:25 -040025
26You can also get help on all the command-line flags the program understands
27by running:
28
Joe Gregorio652898b2011-05-02 21:07:43 -040029 $ python buzz.py --help
Joe Gregorio20a5aa92011-04-01 17:44:25 -040030
31To get detailed log output run:
32
Joe Gregorio652898b2011-05-02 21:07:43 -040033 $ python buzz.py --logging_level=DEBUG
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040034"""
35
36__author__ = 'jcgregorio@google.com (Joe Gregorio)'
37
Joe Gregorio6abf8702011-03-18 22:44:17 -040038import gflags
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040039import httplib2
Joe Gregorio6abf8702011-03-18 22:44:17 -040040import logging
Joe Gregoriof08a4982011-10-07 13:11:16 -040041import os
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040042import pprint
Joe Gregorio6abf8702011-03-18 22:44:17 -040043import sys
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040044
Joe Gregorio6abf8702011-03-18 22:44:17 -040045from apiclient.discovery import build
46from oauth2client.file import Storage
Joe Gregorio7d791212011-05-16 21:58:52 -070047from oauth2client.client import AccessTokenRefreshError
Joe Gregoriof08a4982011-10-07 13:11:16 -040048from oauth2client.client import flow_from_clientsecrets
Joe Gregorio6abf8702011-03-18 22:44:17 -040049from oauth2client.tools import run
50
Joe Gregoriof08a4982011-10-07 13:11:16 -040051
Joe Gregorio6abf8702011-03-18 22:44:17 -040052FLAGS = gflags.FLAGS
Joe Gregorio20a5aa92011-04-01 17:44:25 -040053
Joe Gregoriof08a4982011-10-07 13:11:16 -040054# CLIENT_SECRETS, name of a file containing the OAuth 2.0 information for this
55# application, including client_id and client_secret, which are found
56# on the API Access tab on the Google APIs
57# Console <http://code.google.com/apis/console>
58CLIENT_SECRETS = 'client_secrets.json'
59
60# Helpful message to display in the browser if the CLIENT_SECRETS file
61# is missing.
62MISSING_CLIENT_SECRETS_MESSAGE = """
63WARNING: Please configure OAuth 2.0
64
65To make this sample run you will need to populate the client_secrets.json file
66found at:
67
68 %s
69
70with information from the APIs Console <https://code.google.com/apis/console>.
71
72""" % os.path.join(os.path.dirname(__file__), CLIENT_SECRETS)
73
74# Set up a Flow object to be used if we need to authenticate.
75FLOW = flow_from_clientsecrets(CLIENT_SECRETS,
Joe Gregorio6abf8702011-03-18 22:44:17 -040076 scope='https://www.googleapis.com/auth/buzz',
Joe Gregoriof08a4982011-10-07 13:11:16 -040077 message=MISSING_CLIENT_SECRETS_MESSAGE)
78
Joe Gregorio6abf8702011-03-18 22:44:17 -040079
Joe Gregorio20a5aa92011-04-01 17:44:25 -040080# The gflags module makes defining command-line options easy for
81# applications. Run this program with the '--help' argument to see
82# all the flags that it understands.
Joe Gregorio6abf8702011-03-18 22:44:17 -040083gflags.DEFINE_enum('logging_level', 'ERROR',
84 ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
85 'Set the level of logging detail.')
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040086
Joe Gregorioaf276d22010-12-09 14:26:58 -050087
Joe Gregorio6abf8702011-03-18 22:44:17 -040088def main(argv):
Joe Gregorio20a5aa92011-04-01 17:44:25 -040089 # Let the gflags module process the command-line arguments
Joe Gregorio6abf8702011-03-18 22:44:17 -040090 try:
91 argv = FLAGS(argv)
92 except gflags.FlagsError, e:
93 print '%s\\nUsage: %s ARGS\\n%s' % (e, argv[0], FLAGS)
94 sys.exit(1)
95
Joe Gregorio20a5aa92011-04-01 17:44:25 -040096 # Set the logging according to the command-line flag
Joe Gregorio6abf8702011-03-18 22:44:17 -040097 logging.getLogger().setLevel(getattr(logging, FLAGS.logging_level))
98
Joe Gregorio20a5aa92011-04-01 17:44:25 -040099 # If the Credentials don't exist or are invalid run through the native client
100 # flow. The Storage object will ensure that if successful the good
101 # Credentials will get written back to a file.
Joe Gregoriofffa7d72011-02-18 17:20:39 -0500102 storage = Storage('buzz.dat')
103 credentials = storage.get()
Joe Gregorio562b7312011-09-15 09:06:38 -0400104
Joe Gregorio652898b2011-05-02 21:07:43 -0400105 if credentials is None or credentials.invalid:
Joe Gregorio6abf8702011-03-18 22:44:17 -0400106 credentials = run(FLOW, storage)
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -0400107
Joe Gregorio20a5aa92011-04-01 17:44:25 -0400108 # Create an httplib2.Http object to handle our HTTP requests and authorize it
109 # with our good Credentials.
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -0400110 http = httplib2.Http()
111 http = credentials.authorize(http)
112
Joe Gregorio652898b2011-05-02 21:07:43 -0400113 service = build("buzz", "v1", http=http)
114
Joe Gregorio7d791212011-05-16 21:58:52 -0700115 try:
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -0400116
Joe Gregorio7d791212011-05-16 21:58:52 -0700117 activities = service.activities()
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -0400118
Joe Gregorio7d791212011-05-16 21:58:52 -0700119 # Retrieve the first two activities
120 activitylist = activities.list(
121 max_results='2', scope='@self', userId='@me').execute()
122 print "Retrieved the first two activities"
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -0400123
Joe Gregorio7d791212011-05-16 21:58:52 -0700124 # Retrieve the next two activities
125 if activitylist:
126 activitylist = activities.list_next(activitylist).execute()
127 print "Retrieved the next two activities"
Joe Gregorioa0a52e42011-02-17 17:13:26 -0500128
Joe Gregorio562b7312011-09-15 09:06:38 -0400129 # List the number of followers
130 followers = service.people().list(
131 userId='@me', groupId='@followers').execute(http)
132 print 'Hello, you have %s followers!' % followers['totalResults']
Joe Gregorio652898b2011-05-02 21:07:43 -0400133
Joe Gregorio7d791212011-05-16 21:58:52 -0700134 except AccessTokenRefreshError:
135 print ("The credentials have been revoked or expired, please re-run"
136 "the application to re-authorize")
Joe Gregorio652898b2011-05-02 21:07:43 -0400137
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -0400138if __name__ == '__main__':
Joe Gregorio6abf8702011-03-18 22:44:17 -0400139 main(sys.argv)