Created
December 15, 2011 14:34
-
-
Save fireball2018/1481290 to your computer and use it in GitHub Desktop.
WeiboMixin for tornado
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class WeiboMixin(tornado.auth.OAuth2Mixin): | |
"""Weibo authentication using the new Graph API and OAuth2.""" | |
_OAUTH_ACCESS_TOKEN_URL = "https://api.weibo.com/oauth2/access_token?" | |
_OAUTH_AUTHORIZE_URL = "https://api.weibo.com/oauth2/authorize?" | |
_OAUTH_NO_CALLBACKS = False | |
access_token = "" | |
def get_authenticated_user(self, redirect_uri, client_id, client_secret, | |
code, callback): | |
http = httpclient.AsyncHTTPClient() | |
args = { | |
"redirect_uri": redirect_uri, | |
"code": code, | |
"client_id": client_id, | |
"client_secret": client_secret, | |
} | |
http.fetch(self._oauth_request_token_url(**args), method="POST", body="", | |
callback=self.async_callback(self._on_access_token, redirect_uri, client_id, | |
client_secret, callback)) | |
def _on_access_token(self, redirect_uri, client_id, client_secret, | |
callback, response): | |
if response.error: | |
logging.warning('Weibo auth error: %s' % str(response)) | |
callback(None) | |
return | |
session = escape.json_decode(response.body) | |
self.weibo_request( | |
path="/account/get_uid", | |
callback=self.async_callback( | |
self._on_get_uid, callback, session), | |
access_token=session["access_token"] | |
) | |
def _on_get_uid(self, callback, session, user): | |
if user is None or 'uid' not in user: | |
callback(user) | |
return | |
self.weibo_request( | |
path="/users/show", | |
callback=self.async_callback( | |
self._on_get_user_info, callback, session), | |
access_token=session["access_token"], | |
uid = user.get('uid') | |
) | |
def _on_get_user_info(self, callback, session, user_info): | |
if user_info is None or 'error' in user_info: | |
callback(user_info) | |
return | |
session['lifttime'] = time.time() | |
user_info['access_token'] = session | |
callback(user_info) | |
def weibo_request(self, path, callback, access_token=None, | |
post_args=None, **args): | |
"""Fetches the given relative API path, e.g., "/btaylor/picture" | |
class MainHandler(tornado.web.RequestHandler, | |
tornado.auth.WeiboGraphMixin): | |
@tornado.web.authenticated | |
@tornado.web.asynchronous | |
def get(self): | |
self.weibo_request( | |
"/account/get_uid", | |
access_token=self.current_user["access_token"], | |
callback=self.async_callback(self._on_post)) | |
def _on_post(self, new_entry): | |
if not new_entry: | |
# Call failed; perhaps missing permission? | |
self.authorize_redirect() | |
return | |
self.finish("Posted a message!") | |
""" | |
url = "https://api.weibo.com/2" + path + '.json' | |
all_args = { | |
'access_token': self.access_token | |
} | |
if access_token: | |
all_args["access_token"] = access_token | |
all_args.update(args) | |
# all_args.update(post_args or {}) | |
if all_args: url += "?" + urllib.urlencode(all_args) | |
callback = self.async_callback(self._on_weibo_request, callback) | |
http = httpclient.AsyncHTTPClient() | |
if post_args is not None: | |
http.fetch(url, method="POST", body=urllib.urlencode(post_args), | |
callback=callback) | |
else: | |
http.fetch(url, callback=callback) | |
def _on_weibo_request(self, callback, response): | |
if response.error: | |
logging.warning("Error response %s fetching %s", response.error, | |
response.request.url) | |
logging.warning("body:%s" % response.body) | |
callback(escape.json_decode(response.body)) | |
return | |
callback(escape.json_decode(response.body)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment