blob: e6b2ff49a66055b57ff0de0493ff3473683c78d2 [file] [log] [blame]
Joe Gregorio48d361f2010-08-18 13:19:21 -04001#!/usr/bin/python2.4
2# -*- coding: utf-8 -*-
3#
4# Copyright 2010 Google Inc. All Rights Reserved.
5
6"""One-line documentation for discovery module.
7
8A detailed description of discovery.
9"""
10
11__author__ = 'jcgregorio@google.com (Joe Gregorio)'
12
Joe Gregorio48d361f2010-08-18 13:19:21 -040013
14from apiclient.discovery import build
15
16import httplib2
Joe Gregorio48d361f2010-08-18 13:19:21 -040017import oauth2 as oauth
Joe Gregorio41cf7972010-08-18 15:21:06 -040018import re
19import simplejson
20
Joe Gregorio48d361f2010-08-18 13:19:21 -040021
22def oauth_wrap(consumer, token, http):
23 """
24 Args:
25 http - An instance of httplib2.Http
26 or something that acts like it.
27
28 Returns:
29 A modified instance of http that was passed in.
Joe Gregorio41cf7972010-08-18 15:21:06 -040030
Joe Gregorio48d361f2010-08-18 13:19:21 -040031 Example:
32
33 h = httplib2.Http()
34 h = oauth_wrap(h)
35
36 Grumble. You can't create a new OAuth
37 subclass of httplib2.Authenication because
38 it never gets passed the absolute URI, which is
39 needed for signing. So instead we have to overload
Joe Gregorio41cf7972010-08-18 15:21:06 -040040 'request' with a closure that adds in the
Joe Gregorio48d361f2010-08-18 13:19:21 -040041 Authorization header and then calls the original version
42 of 'request()'.
43 """
44 request_orig = http.request
45 signer = oauth.SignatureMethod_HMAC_SHA1()
46
Joe Gregorio41cf7972010-08-18 15:21:06 -040047 def new_request(uri, method="GET", body=None, headers=None,
48 redirections=httplib2.DEFAULT_MAX_REDIRECTS, connection_type=None):
Joe Gregorio48d361f2010-08-18 13:19:21 -040049 """Modify the request headers to add the appropriate
50 Authorization header."""
51 req = oauth.Request.from_consumer_and_token(
52 consumer, token, http_method=method, http_url=uri)
53 req.sign_request(signer, consumer, token)
54 if headers == None:
55 headers = {}
56 headers.update(req.to_header())
57 headers['user-agent'] = 'jcgregorio-test-client'
Joe Gregorio41cf7972010-08-18 15:21:06 -040058 return request_orig(uri, method, body, headers,
59 redirections, connection_type)
Joe Gregorio48d361f2010-08-18 13:19:21 -040060
61 http.request = new_request
62 return http
63
Joe Gregorio41cf7972010-08-18 15:21:06 -040064
Joe Gregorio48d361f2010-08-18 13:19:21 -040065def get_wrapped_http():
66 f = open("oauth_token.dat", "r")
67 oauth_params = simplejson.loads(f.read())
68
Joe Gregorio41cf7972010-08-18 15:21:06 -040069 consumer = oauth.Consumer(
70 oauth_params['consumer_key'], oauth_params['consumer_secret'])
71 token = oauth.Token(
72 oauth_params['oauth_token'], oauth_params['oauth_token_secret'])
Joe Gregorio48d361f2010-08-18 13:19:21 -040073
Joe Gregorio41cf7972010-08-18 15:21:06 -040074 # Create a simple monkeypatch for httplib2.Http.request
Joe Gregorio48d361f2010-08-18 13:19:21 -040075 # just adds in the oauth authorization header and then calls
76 # the original request().
77 http = httplib2.Http()
78 return oauth_wrap(consumer, token, http)
79
80
81def main():
82 http = get_wrapped_http()
Joe Gregorio41cf7972010-08-18 15:21:06 -040083 p = build("buzz", "v1", http=http)
Joe Gregorio48d361f2010-08-18 13:19:21 -040084 activities = p.activities()
85 activitylist = activities.list(scope='@self', userId='@me')
86 print activitylist['items'][0]['title']
87 activities.insert(userId='@me', body={
88 'title': 'Testing insert',
89 'object': {
Joe Gregorio41cf7972010-08-18 15:21:06 -040090 'content': u'Just a short note to show that insert is working. ☄',
Joe Gregorio48d361f2010-08-18 13:19:21 -040091 'type': 'note'}
92 }
93 )
94
95if __name__ == '__main__':
96 main()