blob: a009517ba89bd1ba7f11cea3871f116782eddec9 [file] [log] [blame]
Joe Gregorio695fdc12011-01-16 16:46:55 -05001#!/usr/bin/python2.4
2# -*- coding: utf-8 -*-
3#
4# Copyright 2010 Google Inc. All Rights Reserved.
5
6"""Simple command-line example for Buzz.
7
8Command-line application that retrieves the users
9latest content and then adds a new entry.
10"""
11
12__author__ = 'jcgregorio@google.com (Joe Gregorio)'
13
Joe Gregorio695fdc12011-01-16 16:46:55 -050014import httplib2
15import pickle
16import pprint
17
Joe Gregoriodeeb0202011-02-15 14:49:57 -050018from apiclient.discovery import build
19from oauth2client.file import Storage
20from oauth2client.client import OAuth2WebServerFlow
21from oauth2client.tools import run
22
Joe Gregorio695fdc12011-01-16 16:46:55 -050023# Uncomment the next line to get very detailed logging
24#httplib2.debuglevel = 4
25
26
27def main():
Joe Gregoriodeeb0202011-02-15 14:49:57 -050028 storage = Storage('buzz.dat')
29 credentials = storage.get()
Joe Gregorio9ce4b622011-02-17 15:32:11 -050030 if credentials is None or credentials.invalid == True:
Joe Gregoriodeeb0202011-02-15 14:49:57 -050031 flow = OAuth2WebServerFlow(
32 client_id='433807057907.apps.googleusercontent.com',
33 client_secret='jigtZpMApkRxncxikFpR+SFg',
34 scope='https://www.googleapis.com/auth/buzz',
35 user_agent='buzz-cmdline-sample/1.0',
36 domain='anonymous',
37 xoauth_displayname='Buzz Client Example App'
38 )
39 credentials = run(flow, storage)
Joe Gregorio695fdc12011-01-16 16:46:55 -050040
41 http = httplib2.Http()
42 http = credentials.authorize(http)
43
Joe Gregoriodeeb0202011-02-15 14:49:57 -050044 p = build("buzz", "v1", http=http,
45 developerKey="AIzaSyDRRpR3GS1F1_jKNNM9HCNd2wJQyPG3oN0")
Joe Gregorio695fdc12011-01-16 16:46:55 -050046 activities = p.activities()
47
48 # Retrieve the first two activities
Joe Gregoriodeeb0202011-02-15 14:49:57 -050049 activitylist = activities.list(
50 max_results='2', scope='@self', userId='@me').execute()
Joe Gregorio695fdc12011-01-16 16:46:55 -050051 print "Retrieved the first two activities"
52
53 # Retrieve the next two activities
54 if activitylist:
55 activitylist = activities.list_next(activitylist).execute()
56 print "Retrieved the next two activities"
57
58 # Add a new activity
59 new_activity_body = {
60 "data": {
61 'title': 'Testing insert',
62 'object': {
Joe Gregoriodeeb0202011-02-15 14:49:57 -050063 'content':
64 u'Just a short note to show that insert is working. ☄',
Joe Gregorio695fdc12011-01-16 16:46:55 -050065 'type': 'note'}
66 }
67 }
68 activity = activities.insert(userId='@me', body=new_activity_body).execute()
69 print "Added a new activity"
70
Joe Gregoriodeeb0202011-02-15 14:49:57 -050071 activitylist = activities.list(
72 max_results='2', scope='@self', userId='@me').execute()
Joe Gregorio695fdc12011-01-16 16:46:55 -050073
74 # Add a comment to that activity
75 comment_body = {
76 "data": {
77 "content": "This is a comment"
78 }
79 }
80 item = activitylist['items'][0]
81 comment = p.comments().insert(
82 userId=item['actor']['id'], postId=item['id'], body=comment_body
83 ).execute()
84 print 'Added a comment to the new activity'
85 pprint.pprint(comment)
86
87if __name__ == '__main__':
88 main()