Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 1 | #!/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 | |
| 8 | A detailed description of discovery. |
| 9 | """ |
| 10 | |
| 11 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 12 | |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 13 | |
| 14 | from apiclient.discovery import build |
| 15 | |
| 16 | import httplib2 |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 17 | import oauth2 as oauth |
Joe Gregorio | 41cf797 | 2010-08-18 15:21:06 -0400 | [diff] [blame] | 18 | import re |
| 19 | import simplejson |
| 20 | |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 21 | |
| 22 | def 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 Gregorio | 41cf797 | 2010-08-18 15:21:06 -0400 | [diff] [blame] | 30 | |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 31 | 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 Gregorio | 41cf797 | 2010-08-18 15:21:06 -0400 | [diff] [blame] | 40 | 'request' with a closure that adds in the |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 41 | 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 Gregorio | 41cf797 | 2010-08-18 15:21:06 -0400 | [diff] [blame] | 47 | def new_request(uri, method="GET", body=None, headers=None, |
| 48 | redirections=httplib2.DEFAULT_MAX_REDIRECTS, connection_type=None): |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 49 | """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 Gregorio | 41cf797 | 2010-08-18 15:21:06 -0400 | [diff] [blame] | 58 | return request_orig(uri, method, body, headers, |
| 59 | redirections, connection_type) |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 60 | |
| 61 | http.request = new_request |
| 62 | return http |
| 63 | |
Joe Gregorio | 41cf797 | 2010-08-18 15:21:06 -0400 | [diff] [blame] | 64 | |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 65 | def get_wrapped_http(): |
| 66 | f = open("oauth_token.dat", "r") |
| 67 | oauth_params = simplejson.loads(f.read()) |
| 68 | |
Joe Gregorio | 41cf797 | 2010-08-18 15:21:06 -0400 | [diff] [blame] | 69 | 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 Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 73 | |
Joe Gregorio | 41cf797 | 2010-08-18 15:21:06 -0400 | [diff] [blame] | 74 | # Create a simple monkeypatch for httplib2.Http.request |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 75 | # 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 | |
| 81 | def main(): |
| 82 | http = get_wrapped_http() |
Joe Gregorio | 41cf797 | 2010-08-18 15:21:06 -0400 | [diff] [blame] | 83 | p = build("buzz", "v1", http=http) |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 84 | 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 Gregorio | 41cf797 | 2010-08-18 15:21:06 -0400 | [diff] [blame] | 90 | 'content': u'Just a short note to show that insert is working. ☄', |
Joe Gregorio | 48d361f | 2010-08-18 13:19:21 -0400 | [diff] [blame] | 91 | 'type': 'note'} |
| 92 | } |
| 93 | ) |
| 94 | |
| 95 | if __name__ == '__main__': |
| 96 | main() |