把 Tornado 升级到4后,发现正常运行的微博登录不可以了。

原因是4已经移除 RequestHandler.async_callback and WebSocketHandler.async_callback ,具体见 http://www.tornadoweb.org/en/stable/releases/v4.0.0.html

替换成 functools.partial 就可解决

记得需要先

import functools

修改后的Weibo.py如下

# -*- coding: utf-8 -*-
import functools
from tornado import gen
from tornado import httpclient
from tornado import escape
from tornado.httputil import url_concat
from tornado.concurrent import Future
from tornado.auth import OAuth2Mixin, _auth_return_future, AuthError try:
import urlparse
except ImportError:
import urllib.parse as urlparse try:
import urllib.parse as urllib_parse
except ImportError:
import urllib as urllib_parse class WeiboMixin(OAuth2Mixin):
_OAUTH_ACCESS_TOKEN_URL = 'https://api.weibo.com/oauth2/access_token'
_OAUTH_AUTHORIZE_URL = 'https://api.weibo.com/oauth2/authorize?' @_auth_return_future
def get_authenticated_user(self, redirect_uri, client_id, client_secret, code, callback, grant_type='authorization_code', extra_fields=None):
http = self.get_auth_http_client()
args = {
'redirect_uri': redirect_uri,
'code': code,
'client_id': client_id,
'client_secret': client_secret,
'grant_type': grant_type,
} fields = set(['id', 'screen_name', 'profile_image_url']) if extra_fields:
fields.update(extra_fields) # self.async_callback
http.fetch(self._OAUTH_ACCESS_TOKEN_URL, method='POST',
body=urllib_parse.urlencode(args),
callback=functools.partial(self._on_access_token, redirect_uri, client_id, client_secret, callback, fields)) def _oauth_request_token_url(self, redirect_uri=None, client_id=None,
client_secret=None, code=None,
grant_type=None, extra_params=None):
pass def _on_access_token(self, redirect_uri, client_id, client_secret,
future, fields, response):
if response.error:
future.set_exception(AuthError('Weibo auth error %s' % str(response)))
return args = escape.json_decode(escape.native_str(response.body))
session = {
'access_token': args['access_token'],
'expires': args['expires_in'],
'uid': args['uid'],
} # self.async_callback
self.weibo_request(
path='/users/show.json',
callback=functools.partial(
self._on_get_user_info, future, session, fields),
access_token=session['access_token'],
uid=session['uid']
) def _on_get_user_info(self, future, session, fields, user):
if user is None:
future.set_result(None)
return fieldmap = {}
for field in fields:
fieldmap[field] = user.get(field) fieldmap.update({'access_token': session['access_token'], 'session_expires': session['expires']}) future.set_result(fieldmap) @_auth_return_future
def weibo_request(self, path, callback, access_token=None, uid=None, post_args=None, **args):
url = "https://api.weibo.com/2" + path
all_args = {}
if access_token:
all_args['access_token'] = access_token
if uid:
all_args['uid'] = uid
if args:
all_args.update(args) if all_args:
url += '?' + urllib_parse.urlencode(all_args)
# self.async_callback
callback = functools.partial(self._on_weibo_request, callback)
http = self.get_auth_http_client()
if post_args is not None:
http.fetch(url, method="POST", body=urllib_parse.urlencode(post_args),
callback=callback)
else:
http.fetch(url, callback=callback) def _on_weibo_request(self, future, response):
if response.error:
future.set_exception(AuthError('Error response %s fetching %s',
response.error, response.request.url)) return future.set_result(escape.json_decode(response.body)) def get_auth_http_client(self):
return httpclient.AsyncHTTPClient()

登录代码

class WeiboAuthHandler(SNSBaseHandler, WeiboMixin):
@tornado.web.asynchronous
@gen.coroutine
def get(self):
if self.get_argument('code', None):
user = yield self.get_authenticated_user(
redirect_uri=self.request.full_url(),
client_id=self.settings['weibo_api_key'],
client_secret=self.settings['weibo_api_secret'],
code=self.get_argument('code')) snsId = user['id']
email = '%s@weibo.com' % snsId
nickname = user['screen_name']
profileImg= 'http://tp1.sinaimg.cn/%s/180/0/1' % snsId
session_expires = int(time.time()) + int(user['session_expires'])
result,tp,userId = self.SNSUser(self.SNSType['weibo'],email,nickname,snsId,user['access_token'],json.dumps(user),session_expires,profileImg)
if result:
expiresDays = int(user['session_expires'])/(3600*24)
expiresDays = expiresDays < 1 and 1 or expiresDays
self.userLogin(userId,expiresDays)
url_next=self.get_argument("next", "/")
self.redirect(url_next)
else:
self.redirect('/login')
else:
self.authorize_redirect(
redirect_uri=self.request.full_url(),
client_id=self.settings['weibo_api_key']
)

升级Tornado到4后weibo oauth登录不了的更多相关文章

  1. Yii2 使用 QQ 和 Weibo 第三方登录源码

    我们社区在 yii2-authclient 多次升级后,登录异常.一直想寻求一种通用的方法,尽量不重写 OAuth2, BaseOAuth 以及 OAuthToken 类, 所以本次直接在 initU ...

  2. 解决Spring Boot 从1.x升级到 2.x 后 单点登陆(SSO)问题

    解决Spring Boot 从1.x升级到 2.x 后 单点登陆(SSO)问题   在学习Spring Cloud 时,遇到了授权服务oauth 相关内容时,总是一知半解,因此决定先把Spring S ...

  3. SharePoint 2010升级到sharePoint 2013后,人员失去对网站的权限的原因及解决方法。The reason and solution for permission lost after the upgrading

    昨天碰到了一个问题,一个网站在从SharePoint 2010升级到SharePoint 2013后,人员都不能登录了,必须重加赋权,人员才能登录,这样非常麻烦. 原因:是认证方式的问题.在Share ...

  4. DZ升级到X3.2后,UCenter用户管理中心进不了了

    前天将DZ升级到X3.2后,UCenter用户管理中心进不了了,输入的密码也对,验证码也对,就是点登录后没反应,又回来输入前的状态.如果更换密码后,显示密码错误,证明密码是没错的.但就是进不了.大家看 ...

  5. [IOS NSUserDefaults]的使用:登陆后不再显示登录界面。

    之前搜了好多地方都没找到实现“登陆后不再显示登录界面,而默认自动登录”的方法. 待我发现有种存储方式叫NSUserDefaults的时候,立马又感觉新技能get了. 简介: NSUserDefault ...

  6. 升级10.11.6后CocoaPods的坑,之前10.11.4已经安装好的,居然没了Failed to locate Homebrew!

    升级10.11.6后CocoaPods的坑,之前10.11.4已经安装好的,居然没了,用命令 sudo gem install cocoapod 装不上,换 sudo gem install -n/u ...

  7. 升级xcode6和ios8后,unity遇到的一些小问题

    升级最新的Xocde6后,如果不是最新版本的unity,虽然也可以也可以正常的build,但如果想通过unity连真机进行profile的话,就会在xocde中报错,这个的主要原因是unity的配置里 ...

  8. xcode7.3 升级 xcode8.0 后权限设置问题(升级xcode 8.0 后构建版本不显示问题)

    xcode7.3 升级 xcode8.0 后权限设置问题(升级xcode 8.0 后构建版本不显示问题) 前两天为了适配 iOS10 的系统 我将xcode 7.3 升级到了 xcode 8.0 但是 ...

  9. Remote Desktop manager 连接后无法自动登录

    现象: Remote Desktop manager 连接后无法自动登录 用Windows 自带的远程桌面 可以自动登录 解决方法: 在指定站点 右键 Edit Entry. 如下图处打勾就可以了.

随机推荐

  1. v8-su-root

    1.下载userdebug版本 2.设置模块打开develop options 3.勾选usb debugging 4.adb remount 5.解压SuperSU_N.7z(联系我索取)并push ...

  2. TCP检验和

    TCP的检验和   检验和目的 目的是为了发现TCP首部和数据在发送端到接收端之间发生的任何改动.如果接收方检测到检验和有差错,则TCP段会被直接丢弃. TCP在计算检验和时,要加上一个12字节的伪首 ...

  3. mipi 调试经验【转】

    转自:http://blog.csdn.net/g_salamander/article/details/9163455 版权声明:本文为博主原创文章,未经博主允许不得转载. 以下是最近几个月在调试 ...

  4. ORACLE表空间查询和管理【转】

    红色是自由指定的~~--查询表空间SELECT D.TABLESPACE_NAME,       SPACE "SUM_SPACE(M)",       SPACE - NVL(F ...

  5. 在VirtualBox上安装Ubuntu

    Windows 10家庭中文版,VirtualBox 5.2.12 r122591,Ubuntu ubuntu-18.04-desktop, 前言 很久没用过Linux类的操作系统了,上一次是好多年前 ...

  6. Web Api - HttpMessageHandler 学习

    参考: 1.http://www.cnblogs.com/chenboyi081/p/4995252.html 2.http://www.cnblogs.com/r01cn/archive/2012/ ...

  7. python网络编程-Select\Poll\Epoll异步IO

    首先列一下,sellect.poll.epoll三者的区别 select select最早于1983年出现在4.2BSD中,它通过一个select()系统调用来监视多个文件描述符的数组,当select ...

  8. 在VS中让一个JS文件智能提示另一个JS文件中的成员2--具体引用

    我们知道,在html中,利用<script language="javascript" type="text/javascript" src=" ...

  9. 洛谷P1168中位数

    传送门啦 基本思想就是二分寻找答案,然后用树状数组去维护有几个比这个二分出来的值大,然后就没有了: 数据要离散,这个好像用map也可以,但是不会: 那怎么离散呢? 我们先把a数组读入并复制给s数组,然 ...

  10. PHP实现菜单无限极分类

    菜单数据 这里我们的菜单数据是临时数据, 没有从数据库中查询处理,数据基本和数据库中的的相似. 数据如下: $items = array( 1 => array('id' => 1, 'p ...