blob: 3d657b952c44e2d099b3f3559f78233c5e5b718e [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
18"""Simple command-line example for Buzz.
19
Joe Gregorio20a5aa92011-04-01 17:44:25 -040020Command-line application that retrieves the users latest content and then adds
21a new entry.
22
23Usage:
24 $ python buzz.py.py
25
26You can also get help on all the command-line flags the program understands
27by running:
28
29 $ python buzz.py.py --help
30
31To get detailed log output run:
32
33 $ python buzz.py.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
46from oauth2client.client import OAuth2WebServerFlow
47from oauth2client.tools import run
48
49FLAGS = gflags.FLAGS
Joe Gregorio20a5aa92011-04-01 17:44:25 -040050
51# Set up a Flow object to be used if we need to authenticate. This
52# sample uses OAuth 2.0, and we set up the OAuth2WebServerFlow with
53# the information it needs to authenticate. Note that it is called
54# the Web Server Flow, but it can also handle the flow for native
55# applications <http://code.google.com/apis/accounts/docs/OAuth2.html#IA>
56# The client_id client_secret are copied from the Identity tab on
57# the Google APIs Console <http://code.google.com/apis/console>
Joe Gregorio6abf8702011-03-18 22:44:17 -040058FLOW = OAuth2WebServerFlow(
59 client_id='433807057907.apps.googleusercontent.com',
60 client_secret='jigtZpMApkRxncxikFpR+SFg',
61 scope='https://www.googleapis.com/auth/buzz',
62 user_agent='buzz-cmdline-sample/1.0')
63
Joe Gregorio20a5aa92011-04-01 17:44:25 -040064# The gflags module makes defining command-line options easy for
65# applications. Run this program with the '--help' argument to see
66# all the flags that it understands.
Joe Gregorio6abf8702011-03-18 22:44:17 -040067gflags.DEFINE_enum('logging_level', 'ERROR',
68 ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
69 'Set the level of logging detail.')
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040070
Joe Gregorioaf276d22010-12-09 14:26:58 -050071
Joe Gregorio6abf8702011-03-18 22:44:17 -040072def main(argv):
Joe Gregorio20a5aa92011-04-01 17:44:25 -040073 # Let the gflags module process the command-line arguments
Joe Gregorio6abf8702011-03-18 22:44:17 -040074 try:
75 argv = FLAGS(argv)
76 except gflags.FlagsError, e:
77 print '%s\\nUsage: %s ARGS\\n%s' % (e, argv[0], FLAGS)
78 sys.exit(1)
79
Joe Gregorio20a5aa92011-04-01 17:44:25 -040080 # Set the logging according to the command-line flag
Joe Gregorio6abf8702011-03-18 22:44:17 -040081 logging.getLogger().setLevel(getattr(logging, FLAGS.logging_level))
82
Joe Gregorio20a5aa92011-04-01 17:44:25 -040083 # If the Credentials don't exist or are invalid run through the native client
84 # flow. The Storage object will ensure that if successful the good
85 # Credentials will get written back to a file.
Joe Gregoriofffa7d72011-02-18 17:20:39 -050086 storage = Storage('buzz.dat')
87 credentials = storage.get()
Joe Gregorioa0a52e42011-02-17 17:13:26 -050088 if credentials is None or credentials.invalid == True:
Joe Gregorio6abf8702011-03-18 22:44:17 -040089 credentials = run(FLOW, storage)
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040090
Joe Gregorio20a5aa92011-04-01 17:44:25 -040091 # Create an httplib2.Http object to handle our HTTP requests and authorize it
92 # with our good Credentials.
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040093 http = httplib2.Http()
94 http = credentials.authorize(http)
95
Joe Gregorio20a5aa92011-04-01 17:44:25 -040096 # Build a service object for interacting with the API. Visit
97 # the Google APIs Console <http://code.google.com/apis/console>
98 # to get a developerKey for your own application.
Joe Gregorio1ae3e742011-02-25 15:17:14 -050099 service = build("buzz", "v1", http=http,
Joe Gregorio6abf8702011-03-18 22:44:17 -0400100 developerKey="AIzaSyDRRpR3GS1F1_jKNNM9HCNd2wJQyPG3oN0")
Joe Gregorio1ae3e742011-02-25 15:17:14 -0500101 activities = service.activities()
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -0400102
Joe Gregorio6abf8702011-03-18 22:44:17 -0400103 # Retrieve the first two activities
104 activitylist = activities.list(
105 max_results='2', scope='@self', userId='@me').execute()
106 print "Retrieved the first two activities"
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -0400107
Joe Gregorio6abf8702011-03-18 22:44:17 -0400108 # Retrieve the next two activities
109 if activitylist:
110 activitylist = activities.list_next(activitylist).execute()
111 print "Retrieved the next two activities"
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -0400112
Joe Gregorio6abf8702011-03-18 22:44:17 -0400113 # Add a new activity
114 new_activity_body = {
115 'title': 'Testing insert',
116 'object': {
117 'content':
118 u'Just a short note to show that insert is working. ☄',
119 'type': 'note'}
120 }
121 activity = activities.insert(userId='@me', body=new_activity_body).execute()
122 print "Added a new activity"
Joe Gregorioa0a52e42011-02-17 17:13:26 -0500123
Joe Gregorio6abf8702011-03-18 22:44:17 -0400124 activitylist = activities.list(
125 max_results='2', scope='@self', userId='@me').execute()
Joe Gregorioa0a52e42011-02-17 17:13:26 -0500126
Joe Gregorio6abf8702011-03-18 22:44:17 -0400127 # Add a comment to that activity
128 comment_body = {
129 "content": "This is a comment"
130 }
131 item = activitylist['items'][0]
132 comment = service.comments().insert(
133 userId=item['actor']['id'], postId=item['id'], body=comment_body
134 ).execute()
135 print 'Added a comment to the new activity'
136 pprint.pprint(comment)
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -0400137
138if __name__ == '__main__':
Joe Gregorio6abf8702011-03-18 22:44:17 -0400139 main(sys.argv)