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