最近微信登录开放公测,为了方便微信用户使用,我们的产品也决定加上微信登录功能,然后就有了这篇笔记。
根据需求选择相应的登录方式
微信现在提供两种登录接入方式
移动应用微信登录网站应用微信登录这里我们使用的是网站应用微信登录
按照官方流程
1 注册并通过开放平台开发者资质认证注册微信开放平台帐号后,在帐号中心中填写开发者资质认证申请,并等待认证通过。
2 创建网站应用通过填写网站应用名称、简介和图标,以及各平台下载地址等资料,创建网站应用
3 接入微信登录在资源中心查阅网站应用开发文档,开发接入微信登陆功能,让用户可使用微信登录你的网站应用
如果已经完成上面的操作,请继续往下看
微信网站应用微信登录是基于OAuth2.0协议标准构建的微信OAuth2.0授权登录系统。
微信OAuth2.0授权登录目前支持authorization_code模式,适用于拥有server端的应用授权。该模式整体流程为:
第三方发起微信授权登录请求,微信用户允许授权第三方应用后,微信会拉起应用或重定向到第三方网站,并且带上授权临时票据code参数;
通过code参数加上AppID和AppSecret等,通过API换取access_token;
通过access_token进行接口调用,获取用户基本数据资源或帮助用户实现基本操作。
具体流程请参考官方文档,我们这里只说一下python的实现方法。官方文档地址点这里
参考python-instagram 我写了一个python-weixin (https://github.com/zongxiao/python-weixin)一个微信python SDK
不过现在还只有微信接入、获取用户信息、刷新refresh_token等简单功能
首先 需要把代码clone到本地
然后执行
python setup.py install
使用方式非常简单
1fromweixin.clientimportWeixinAPI23APP_ID ='your app id'4APP_SECRET ='your app secret'5REDIRECT_URI ='http://your_domain.com/redirect_uri'#这里一定要注意 地址一定要加上http/https67scope = ("snsapi_login", )8api = WeixinAPI(appid=APP_ID,9app_secret=APP_SECRET,10redirect_uri=REDIRECT_URI)1112authorize_url = api.get_authorize_url(scope=scope)
现在将
authorize_url 地址在浏览器打开, 将跳转到微信登录页面,使用手机扫码登录后将跳转到
http://your_domain.com/redirect_uri?code=CODE&state=STATE 页面
现在我们就可以使用code 来获取登录的 access_token
access_token = api.exchange_code_for_access_token(code=code)
access_token 信息为
{"access_token":"ACCESS_TOKEN","expires_in":7200,"refresh_token":"REFRESH_TOKEN","openid":"OPENID","scope":"SCOPE"}
参数说明access_token
接口调用凭证(有效期目前为2个小时)
expires_in
access_token接口调用凭证超时时间,单位(秒)
refresh_token
用户刷新access_token(有效期目前为30天)
openid
授权用户唯一标识
scope
用户授权的作用域,使用逗号(,)分隔
获取access_token后,就可以进行接口调用,有以下前提:
access_token有效且未超时;
微信用户已授权给第三方应用帐号相应接口作用域(scope)。
对于接口作用域(scope),能调用的接口有以下:
授权作用域(scope)接口接口说明snsapi_base
/sns/oauth2/access_token
通过code换取access_token、refresh_token和已授权scope
/sns/oauth2/refresh_token
刷新或续期access_token使用
/sns/auth
检查access_token有效性
snsapi_userinfo
/sns/userinfo
获取用户个人信息
api = WeixinAPI(appid=APP_ID,
app_secret=APP_SECRET,
redirect_uri=REDIRECT_URI)#刷新或续期access_token使用refresh_token = api.exchange_refresh_token_for_access_token(refresh_token=auth_info['refresh_token'])
api= WeixinAPI(access_token=auth_info['access_token'])#获取用户个人信息user = api.user(openid=auth_info['openid'])#检查access_token有效性v = api.validate_token(openid=auth_info['openid'])
现在就微信登录就完成了
下面是用 flask 实现的完整的例子
fromflaskimportFlaskfromflaskimportMarkupfromflaskimportredirectfromflaskimportrequestfromflaskimportjsonifyfromweixin.clientimportWeixinAPIfromweixin.oauth2importOAuth2AuthExchangeError
app= Flask(__name__)
APP_ID='appid'APP_SECRET='app secret'REDIRECT_URI='http://localhost.com/authorization'@app.route("/authorization")defauthorization():
code= request.args.get('code')
api= WeixinAPI(appid=APP_ID,
app_secret=APP_SECRET,
redirect_uri=REDIRECT_URI)
auth_info= api.exchange_code_for_access_token(code=code)
api= WeixinAPI(access_token=auth_info['access_token'])
resp= api.user(openid=auth_info['openid'])returnjsonify(resp)
@app.route("/login")deflogin():
api= WeixinAPI(appid=APP_ID,
app_secret=APP_SECRET,
redirect_uri=REDIRECT_URI)
redirect_uri= api.get_authorize_login_url(scope=("snsapi_login",))returnredirect(redirect_uri)
@app.route("/")defhello():returnMarkup('<a href="%s">weixin login!</a>') %'/login'if__name__=="__main__":
app.run(debug=True)
参考链接:
微信网站应用接入文档
网站应用创建地址
python-weixin github 地址 https://github.com/zongxiao/python-weixin