blob: c769576047bbacfa71a6cfcd00990b8041129f6b [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
Joe Gregoriof4153422011-03-18 22:45:18 -040021import gflags
22import sys
Joe Gregorio292b9b82011-01-12 11:36:11 -050023import httplib2
Joe Gregoriof4153422011-03-18 22:45:18 -040024import logging
25import os
Joe Gregorio292b9b82011-01-12 11:36:11 -050026import pprint
Joe Gregoriof4153422011-03-18 22:45:18 -040027import sys
Joe Gregorio292b9b82011-01-12 11:36:11 -050028
Joe Gregoriof4153422011-03-18 22:45:18 -040029FLAGS = gflags.FLAGS
30
31gflags.DEFINE_enum('logging_level', 'ERROR',
32 ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
33 'Set the level of logging detail.')
Joe Gregorio292b9b82011-01-12 11:36:11 -050034
35
Joe Gregoriof4153422011-03-18 22:45:18 -040036def main(argv):
37 try:
38 argv = FLAGS(argv)
39 except gflags.FlagsError, e:
40 print '%s\\nUsage: %s ARGS\\n%s' % (e, argv[0], FLAGS)
41 sys.exit(1)
42
43 logging.getLogger().setLevel(getattr(logging, FLAGS.logging_level))
44
Joe Gregoriofffa7d72011-02-18 17:20:39 -050045 storage = Storage('buzz.dat')
46 credentials = storage.get()
47 if credentials is None or credentials.invalid == True:
48 buzz_discovery = build("buzz", "v1").auth_discovery()
Joe Gregoriofffa7d72011-02-18 17:20:39 -050049 flow = FlowThreeLegged(buzz_discovery,
Joe Gregoriof4153422011-03-18 22:45:18 -040050 consumer_key='anonymous',
51 consumer_secret='anonymous',
52 user_agent='python-buzz-sample/1.0',
53 domain='anonymous',
54 scope='https://www.googleapis.com/auth/buzz',
55 xoauth_displayname='Google API Client Example App')
Joe Gregoriofffa7d72011-02-18 17:20:39 -050056 credentials = run(flow, storage)
Joe Gregorio292b9b82011-01-12 11:36:11 -050057
58 http = httplib2.Http()
59 http = credentials.authorize(http)
60
61 # Load the local copy of the discovery document
Joe Gregoriof4153422011-03-18 22:45:18 -040062 f = file(os.path.join(os.path.dirname(__file__), "buzz.json"), "r")
Joe Gregorio292b9b82011-01-12 11:36:11 -050063 discovery = f.read()
64 f.close()
65
66 # Optionally load a futures discovery document
Joe Gregoriof4153422011-03-18 22:45:18 -040067 f = file(os.path.join(os.path.dirname(__file__), "../../apiclient/contrib/buzz/future.json"), "r")
Joe Gregorio292b9b82011-01-12 11:36:11 -050068 future = f.read()
69 f.close()
70
71 # Construct a service from the local documents
Joe Gregorio1ae3e742011-02-25 15:17:14 -050072 service = build_from_document(discovery,
Joe Gregorio292b9b82011-01-12 11:36:11 -050073 base="https://www.googleapis.com/",
74 future=future,
75 http=http,
76 developerKey="AIzaSyDRRpR3GS1F1_jKNNM9HCNd2wJQyPG3oN0")
Joe Gregorio1ae3e742011-02-25 15:17:14 -050077 activities = service.activities()
Joe Gregorio292b9b82011-01-12 11:36:11 -050078
Joe Gregoriofffa7d72011-02-18 17:20:39 -050079 try:
80 # Retrieve the first two activities
81 activitylist = activities.list(
82 max_results='2', scope='@self', userId='@me').execute()
83 print "Retrieved the first two activities"
84 except CredentialsInvalidError:
85 print 'Your credentials are no longer valid.'
86 print 'Please re-run this application to re-authorize.'
Joe Gregorio292b9b82011-01-12 11:36:11 -050087
Joe Gregorio292b9b82011-01-12 11:36:11 -050088
89if __name__ == '__main__':
Joe Gregoriof4153422011-03-18 22:45:18 -040090 main(sys.argv)