blob: 2fb4e8583d447c90ee73608f5c8d83cc2f8fbb13 [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
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040041import pprint
Joe Gregorio6abf8702011-03-18 22:44:17 -040042import sys
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040043
Joe Gregorio6abf8702011-03-18 22:44:17 -040044from apiclient.discovery import build
45from oauth2client.file import Storage
Joe Gregorio7d791212011-05-16 21:58:52 -070046from oauth2client.client import AccessTokenRefreshError
Joe Gregorio6abf8702011-03-18 22:44:17 -040047from oauth2client.client import OAuth2WebServerFlow
48from oauth2client.tools import run
49
50FLAGS = gflags.FLAGS
Joe Gregorio20a5aa92011-04-01 17:44:25 -040051
52# Set up a Flow object to be used if we need to authenticate. This
53# sample uses OAuth 2.0, and we set up the OAuth2WebServerFlow with
54# the information it needs to authenticate. Note that it is called
55# the Web Server Flow, but it can also handle the flow for native
56# applications <http://code.google.com/apis/accounts/docs/OAuth2.html#IA>
Joe Gregorio652898b2011-05-02 21:07:43 -040057# The client_id client_secret are copied from the API Access tab on
58# the Google APIs Console <http://code.google.com/apis/console>. When
59# creating credentials for this application be sure to choose an Application
60# type of "Installed application".
Joe Gregorio6abf8702011-03-18 22:44:17 -040061FLOW = OAuth2WebServerFlow(
62 client_id='433807057907.apps.googleusercontent.com',
63 client_secret='jigtZpMApkRxncxikFpR+SFg',
64 scope='https://www.googleapis.com/auth/buzz',
65 user_agent='buzz-cmdline-sample/1.0')
66
Joe Gregorio20a5aa92011-04-01 17:44:25 -040067# The gflags module makes defining command-line options easy for
68# applications. Run this program with the '--help' argument to see
69# all the flags that it understands.
Joe Gregorio6abf8702011-03-18 22:44:17 -040070gflags.DEFINE_enum('logging_level', 'ERROR',
71 ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
72 'Set the level of logging detail.')
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040073
Joe Gregorioaf276d22010-12-09 14:26:58 -050074
Joe Gregorio6abf8702011-03-18 22:44:17 -040075def main(argv):
Joe Gregorio20a5aa92011-04-01 17:44:25 -040076 # Let the gflags module process the command-line arguments
Joe Gregorio6abf8702011-03-18 22:44:17 -040077 try:
78 argv = FLAGS(argv)
79 except gflags.FlagsError, e:
80 print '%s\\nUsage: %s ARGS\\n%s' % (e, argv[0], FLAGS)
81 sys.exit(1)
82
Joe Gregorio20a5aa92011-04-01 17:44:25 -040083 # Set the logging according to the command-line flag
Joe Gregorio6abf8702011-03-18 22:44:17 -040084 logging.getLogger().setLevel(getattr(logging, FLAGS.logging_level))
85
Joe Gregorio20a5aa92011-04-01 17:44:25 -040086 # If the Credentials don't exist or are invalid run through the native client
87 # flow. The Storage object will ensure that if successful the good
88 # Credentials will get written back to a file.
Joe Gregoriofffa7d72011-02-18 17:20:39 -050089 storage = Storage('buzz.dat')
90 credentials = storage.get()
Joe Gregorio652898b2011-05-02 21:07:43 -040091 if credentials is None or credentials.invalid:
Joe Gregorio6abf8702011-03-18 22:44:17 -040092 credentials = run(FLOW, storage)
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040093
Joe Gregorio20a5aa92011-04-01 17:44:25 -040094 # Create an httplib2.Http object to handle our HTTP requests and authorize it
95 # with our good Credentials.
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040096 http = httplib2.Http()
97 http = credentials.authorize(http)
98
Joe Gregorio652898b2011-05-02 21:07:43 -040099 service = build("buzz", "v1", http=http)
100
Joe Gregorio7d791212011-05-16 21:58:52 -0700101 try:
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -0400102
Joe Gregorio7d791212011-05-16 21:58:52 -0700103 activities = service.activities()
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -0400104
Joe Gregorio7d791212011-05-16 21:58:52 -0700105 # Retrieve the first two activities
106 activitylist = activities.list(
107 max_results='2', scope='@self', userId='@me').execute()
108 print "Retrieved the first two activities"
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -0400109
Joe Gregorio7d791212011-05-16 21:58:52 -0700110 # Retrieve the next two activities
111 if activitylist:
112 activitylist = activities.list_next(activitylist).execute()
113 print "Retrieved the next two activities"
Joe Gregorioa0a52e42011-02-17 17:13:26 -0500114
Joe Gregorio7d791212011-05-16 21:58:52 -0700115 # Add a new activity
116 new_activity_body = {
117 'title': 'Testing insert',
118 'object': {
119 'content':
120 u'Just a short note to show that insert is working. ☄',
121 'type': 'note'}
122 }
123 activity = activities.insert(userId='@me', body=new_activity_body).execute()
124 print "Added a new activity"
Joe Gregorioa0a52e42011-02-17 17:13:26 -0500125
Joe Gregorio7d791212011-05-16 21:58:52 -0700126 activitylist = activities.list(
127 max_results='2', scope='@self', userId='@me').execute()
128
129 # Add a comment to that activity
130 comment_body = {
131 "content": "This is a comment"
132 }
133 item = activitylist['items'][0]
134 comment = service.comments().insert(
135 userId=item['actor']['id'], postId=item['id'], body=comment_body
136 ).execute()
137 print 'Added a comment to the new activity'
138 pprint.pprint(comment)
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -0400139
Joe Gregorio652898b2011-05-02 21:07:43 -0400140
Joe Gregorio7d791212011-05-16 21:58:52 -0700141 except AccessTokenRefreshError:
142 print ("The credentials have been revoked or expired, please re-run"
143 "the application to re-authorize")
Joe Gregorio652898b2011-05-02 21:07:43 -0400144
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -0400145if __name__ == '__main__':
Joe Gregorio6abf8702011-03-18 22:44:17 -0400146 main(sys.argv)