blob: 769057e6a90a880270eb41e4ba58d68c4446e651 [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
Joe Gregorioa0a52e42011-02-17 17:13:26 -050017from apiclient.ext.file import Storage
18from apiclient.oauth import CredentialsInvalidError
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040019
20import httplib2
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040021import 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 Gregoriofffa7d72011-02-18 17:20:39 -050028 storage = Storage('buzz.dat')
29 credentials = storage.get()
Joe Gregorioa0a52e42011-02-17 17:13:26 -050030 if credentials is None or credentials.invalid == True:
Joe Gregoriod81ecb02011-02-15 17:46:47 -050031 buzz_discovery = build("buzz", "v1").auth_discovery()
32
33 flow = FlowThreeLegged(buzz_discovery,
34 consumer_key='anonymous',
35 consumer_secret='anonymous',
Joe Gregorio9ce4b622011-02-17 15:32:11 -050036 user_agent='python-buzz-sample/1.0',
Joe Gregoriod81ecb02011-02-15 17:46:47 -050037 domain='anonymous',
38 scope='https://www.googleapis.com/auth/buzz',
39 xoauth_displayname='Google API Client Example App')
40
Joe Gregoriofffa7d72011-02-18 17:20:39 -050041 credentials = run(flow, storage)
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040042
43 http = httplib2.Http()
44 http = credentials.authorize(http)
45
Joe Gregorio88243be2011-01-11 14:13:06 -050046 p = build("buzz", "v1", http=http,
47 developerKey="AIzaSyDRRpR3GS1F1_jKNNM9HCNd2wJQyPG3oN0")
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040048 activities = p.activities()
49
Joe Gregorioa0a52e42011-02-17 17:13:26 -050050 try:
51 # Retrieve the first two activities
52 activitylist = activities.list(
53 max_results='2', scope='@self', userId='@me').execute()
54 print "Retrieved the first two activities"
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040055
Joe Gregorioa0a52e42011-02-17 17:13:26 -050056 # Retrieve the next two activities
57 if activitylist:
58 activitylist = activities.list_next(activitylist).execute()
59 print "Retrieved the next two activities"
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040060
Joe Gregorioa0a52e42011-02-17 17:13:26 -050061 # Add a new activity
62 new_activity_body = {
63 "data": {
64 'title': 'Testing insert',
65 'object': {
66 'content':
67 u'Just a short note to show that insert is working. ☄',
68 'type': 'note'}
Joe Gregoriobc2ff9b2010-11-08 09:20:48 -050069 }
Joe Gregorioa0a52e42011-02-17 17:13:26 -050070 }
71 activity = activities.insert(
72 userId='@me', body=new_activity_body).execute()
73 print "Added a new activity"
74
75 activitylist = activities.list(
76 max_results='2', scope='@self', userId='@me').execute()
77
78 # Add a comment to that activity
79 comment_body = {
80 "data": {
81 "content": "This is a comment"
82 }
83 }
84 item = activitylist['items'][0]
85 comment = p.comments().insert(
86 userId=item['actor']['id'], postId=item['id'], body=comment_body
87 ).execute()
88 print 'Added a comment to the new activity'
89 pprint.pprint(comment)
90 except CredentialsInvalidError:
91 print 'Your credentials are no longer valid.'
92 print 'Please re-run this application to re-authorize.'
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040093
94if __name__ == '__main__':
95 main()