blob: 544655e32491bb53e53e1f456c4b936cb0cb4825 [file] [log] [blame]
ade@google.comac41d842010-08-20 17:11:25 +01001# Copyright (C) 2010 Google Inc.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15__author__ = 'ade@google.com'
16
17import apiclient.discovery
18import logging
19import oauth_wrap
20import oauth2 as oauth
21import urllib
22import urlparse
23
24try:
25 from urlparse import parse_qs, parse_qsl, urlparse
26except ImportError:
27 from cgi import parse_qs, parse_qsl
28
29# TODO(ade) Replace user-agent with something specific
30HEADERS = {
31 'user-agent': 'gdata-python-v3-sample-client/0.1',
32 'content-type': 'application/x-www-form-urlencoded'
33 }
34
35REQUEST_TOKEN_URL = 'https://www.google.com/accounts/OAuthGetRequestToken?domain=anonymous&scope=https://www.googleapis.com/auth/buzz'
36AUTHORIZE_URL = 'https://www.google.com/buzz/api/auth/OAuthAuthorizeToken?domain=anonymous&scope=https://www.googleapis.com/auth/buzz'
37ACCESS_TOKEN_URL = 'https://www.google.com/accounts/OAuthGetAccessToken'
38
39class BuzzGaeClient(object):
40 def __init__(self, consumer_key='anonymous', consumer_secret='anonymous'):
41 self.consumer = oauth.Consumer(consumer_key, consumer_secret)
42 self.consumer_key = consumer_key
43 self.consumer_secret = consumer_secret
44
45 def _make_post_request(self, client, url, parameters):
46 resp, content = client.request(url, 'POST', headers=HEADERS,
47 body=urllib.urlencode(parameters, True))
48
49 if resp['status'] != '200':
50 logging.warn('Request: %s failed with status: %s. Content was: %s' % (url, resp['status'], content))
51 raise Exception('Invalid response %s.' % resp['status'])
52 return resp, content
53
54 def get_request_token(self, callback_url, display_name = None):
55 parameters = {
56 'oauth_callback': callback_url
57 }
58
59 if display_name is not None:
60 parameters['xoauth_displayname'] = display_name
61
62 client = oauth.Client(self.consumer)
63 resp, content = self._make_post_request(client, REQUEST_TOKEN_URL, parameters)
64
65 request_token = dict(parse_qsl(content))
66 return request_token
67
68 def generate_authorisation_url(self, request_token):
69 """Returns the URL the user should be redirected to in other to gain access to their account."""
70
71 base_url = urlparse.urlparse(AUTHORIZE_URL)
72 query = parse_qs(base_url.query)
73 query['oauth_token'] = request_token['oauth_token']
74
75 logging.info(urllib.urlencode(query, True))
76
77 url = (base_url.scheme, base_url.netloc, base_url.path, base_url.params,
78 urllib.urlencode(query, True), base_url.fragment)
79 authorisation_url = urlparse.urlunparse(url)
80 return authorisation_url
81
82 def upgrade_to_access_token(self, request_token, oauth_verifier):
83 token = oauth.Token(request_token['oauth_token'],
84 request_token['oauth_token_secret'])
85 token.set_verifier(oauth_verifier)
86 client = oauth.Client(self.consumer, token)
87
88 parameters = {}
89 resp, content = self._make_post_request(client, ACCESS_TOKEN_URL, parameters)
90 access_token = dict(parse_qsl(content))
91
92 d = {
93 'consumer_key' : self.consumer_key,
94 'consumer_secret' : self.consumer_secret
95 }
96 d.update(access_token)
97 return d
98
ade@google.come80fec82010-08-20 23:42:08 +010099 def build_api_client(self, oauth_params=None):
100 if oauth_params is not None:
101 http = oauth_wrap.get_authorised_http(oauth_params)
102 return apiclient.discovery.build('buzz', 'v1', http = http)
103 else:
104 return apiclient.discovery.build('buzz', 'v1')