-
Notifications
You must be signed in to change notification settings - Fork 401
微信登陆
zwc edited this page Feb 27, 2018
·
3 revisions
- 支持snsapi_base, 以snsapi_base为scope发起的网页授权,是用来获取进入页面的用户的openid的,并且是静默授权并自动跳转到回调页的。用户感知的就是直接进入了回调页(往往是业务页面)
- 支持snsapi_userinfo, 以snsapi_userinfo为scope发起的网页授权,是用来获取用户的基本信息的。但这种授权需要用户手动同意,并且由于用户同意过,所以无须关注,就可在授权后获取该用户的基本信息。
微信详细文档请点击
from weixin import WeixinLogin
# from weixin.login import WeixinLogin
login = WeixinLogin('app_id', 'app_key')
url = login.authorize("http://code.show/#/weixin/callback", "snsapi_base")
data = login.access_token(code)
print data.access_token
print data.refresh_token
print data.openid
如果是snsapi_userinfo则需要
login.user_info(data.access_token)
login.refresh_token(data.refresh_token)
login.jscode2session(js_code
# -*- coding: utf-8 -*-
from datetime import datetime, timedelta
from flask import Flask, redirect, request, url_for
from weixin.login import WeixinLogin
app = Flask(__name__)
app_id = ''
app_secret = ''
wx_login = WeixinLogin(app_id, app_secret)
@app.route("/#")
def login():
openid = request.cookies.get("openid")
next = request.args.get("next") or request.referrer or "/",
if openid:
return redirect(next)
callback = url_for("authorized", next=next, _external=True)
url = wx_login.authorize(callback, "snsapi_base")
return redirect(url)
@app.route("/authorized")
def authorized():
code = request.args.get("code")
if not code:
return "ERR_INVALID_CODE", 400
next = request.args.get("next", "/")
data = wx_login.access_token(code)
openid = data.openid
resp = redirect(next)
expires = datetime.now() + timedelta(days=1)
resp.set_cookie("openid", openid, expires=expires)
return resp