blob: a4f6840b3745034c16851aeb9224cbc2bfc1569b [file] [log] [blame]
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -04001#!/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
14from apiclient.discovery import build
Joe Gregoriod81ecb02011-02-15 17:46:47 -050015from apiclient.oauth import FlowThreeLegged
16from apiclient.ext.authtools import run
17
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040018
19import httplib2
20import pickle
21import pprint
22
23# Uncomment the next line to get very detailed logging
Joe Gregoriobc2ff9b2010-11-08 09:20:48 -050024#httplib2.debuglevel = 4
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040025
Joe Gregorioaf276d22010-12-09 14:26:58 -050026
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040027def main():
Joe Gregoriod81ecb02011-02-15 17:46:47 -050028 try:
29 f = open("buzz.dat", "r")
30 credentials = pickle.loads(f.read())
31 f.close()
32 except:
33 buzz_discovery = build("buzz", "v1").auth_discovery()
34
35 flow = FlowThreeLegged(buzz_discovery,
36 consumer_key='anonymous',
37 consumer_secret='anonymous',
Joe Gregorio9ce4b622011-02-17 15:32:11 -050038 user_agent='python-buzz-sample/1.0',
Joe Gregoriod81ecb02011-02-15 17:46:47 -050039 domain='anonymous',
40 scope='https://www.googleapis.com/auth/buzz',
41 xoauth_displayname='Google API Client Example App')
42
43 credentials = run(flow, 'buzz.dat')
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040044
45 http = httplib2.Http()
46 http = credentials.authorize(http)
47
Joe Gregorio88243be2011-01-11 14:13:06 -050048 p = build("buzz", "v1", http=http,
49 developerKey="AIzaSyDRRpR3GS1F1_jKNNM9HCNd2wJQyPG3oN0")
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040050 activities = p.activities()
51
52 # Retrieve the first two activities
Joe Gregorio88243be2011-01-11 14:13:06 -050053 activitylist = activities.list(
54 max_results='2', scope='@self', userId='@me').execute()
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040055 print "Retrieved the first two activities"
56
57 # Retrieve the next two activities
Joe Gregorio913e70d2010-11-05 15:38:23 -040058 if activitylist:
59 activitylist = activities.list_next(activitylist).execute()
60 print "Retrieved the next two activities"
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040061
62 # Add a new activity
63 new_activity_body = {
Joe Gregorio913e70d2010-11-05 15:38:23 -040064 "data": {
Joe Gregorio88243be2011-01-11 14:13:06 -050065 'title': 'Testing insert',
66 'object': {
67 'content': u'Just a short note to show that insert is working. ☄',
68 'type': 'note'}
69 }
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040070 }
71 activity = activities.insert(userId='@me', body=new_activity_body).execute()
72 print "Added a new activity"
73
Joe Gregorio88243be2011-01-11 14:13:06 -050074 activitylist = activities.list(
75 max_results='2', scope='@self', userId='@me').execute()
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040076
77 # Add a comment to that activity
78 comment_body = {
Joe Gregoriobc2ff9b2010-11-08 09:20:48 -050079 "data": {
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040080 "content": "This is a comment"
Joe Gregoriobc2ff9b2010-11-08 09:20:48 -050081 }
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040082 }
83 item = activitylist['items'][0]
84 comment = p.comments().insert(
85 userId=item['actor']['id'], postId=item['id'], body=comment_body
86 ).execute()
87 print 'Added a comment to the new activity'
88 pprint.pprint(comment)
89
90if __name__ == '__main__':
91 main()