You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
1000 B
40 lines
1000 B
# -*- coding: utf-8 -*-
|
|
|
|
# import weixin
|
|
from datetime import datetime, timedelta
|
|
from flask import Flask, redirect, request, url_for
|
|
from weixin import WeixinLogin
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
app_id = 'wx13d6b82e0cf2bd9b'
|
|
app_secret = ''
|
|
wx_login = WeixinLogin(app_id, app_secret)
|
|
|
|
|
|
@app.route("/login")
|
|
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
|