blob: 8b797f7167532f44a22f4a16a629af25e5fa171e [file] [log] [blame]
Joe Gregorio292b9b82011-01-12 11:36:11 -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 Gregoriofffa7d72011-02-18 17:20:39 -050014from apiclient.discovery import build
Joe Gregorio292b9b82011-01-12 11:36:11 -050015from apiclient.discovery import build_from_document
Joe Gregoriofffa7d72011-02-18 17:20:39 -050016from apiclient.oauth import FlowThreeLegged
17from apiclient.ext.authtools import run
18from apiclient.ext.file import Storage
19from apiclient.oauth import CredentialsInvalidError
Joe Gregorio292b9b82011-01-12 11:36:11 -050020
21import httplib2
Joe Gregorio292b9b82011-01-12 11:36:11 -050022import pprint
23
24# Uncomment the next line to get very detailed logging
25#httplib2.debuglevel = 4
26
27
28def main():
Joe Gregoriofffa7d72011-02-18 17:20:39 -050029 storage = Storage('buzz.dat')
30 credentials = storage.get()
31 if credentials is None or credentials.invalid == True:
32 buzz_discovery = build("buzz", "v1").auth_discovery()
33
34 flow = FlowThreeLegged(buzz_discovery,
35 consumer_key='anonymous',
36 consumer_secret='anonymous',
37 user_agent='python-buzz-sample/1.0',
38 domain='anonymous',
39 scope='https://www.googleapis.com/auth/buzz',
40 xoauth_displayname='Google API Client Example App')
41
42 credentials = run(flow, storage)
Joe Gregorio292b9b82011-01-12 11:36:11 -050043
44 http = httplib2.Http()
45 http = credentials.authorize(http)
46
47 # Load the local copy of the discovery document
48 f = file("buzz.json", "r")
49 discovery = f.read()
50 f.close()
51
52 # Optionally load a futures discovery document
53 f = file("../../apiclient/contrib/buzz/future.json", "r")
54 future = f.read()
55 f.close()
56
57 # Construct a service from the local documents
58 p = build_from_document(discovery,
59 base="https://www.googleapis.com/",
60 future=future,
61 http=http,
62 developerKey="AIzaSyDRRpR3GS1F1_jKNNM9HCNd2wJQyPG3oN0")
63 activities = p.activities()
64
Joe Gregoriofffa7d72011-02-18 17:20:39 -050065 try:
66 # Retrieve the first two activities
67 activitylist = activities.list(
68 max_results='2', scope='@self', userId='@me').execute()
69 print "Retrieved the first two activities"
70 except CredentialsInvalidError:
71 print 'Your credentials are no longer valid.'
72 print 'Please re-run this application to re-authorize.'
Joe Gregorio292b9b82011-01-12 11:36:11 -050073
Joe Gregorio292b9b82011-01-12 11:36:11 -050074
75if __name__ == '__main__':
76 main()