微信登录流程图:
视频效果:
flask项目实现微信登录
1. view代码
- from flask import Flask, render_template
- from flask import Markup
- from flask import redirect
- from flask import request
- from flask import jsonify
-
- from python_weixin_master.weixin.client import WeixinAPI
-
- # from python_weixin_master.weixin.oauth2 import OAuth2AuthExchangeError
-
- app = Flask(__name__)
-
- APP_ID = 'wxa77a00333cd8d99a'
- APP_SECRET = '6f616f770203c4cc930829f6fdfc2396'
- REDIRECT_URI = 'http://127.0.0.1:5000/role'
-
-
- @app.route("/role")
- def role():
- 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)
- """
- auth_info = {
- 'access_token': 'OezXcEiiBSKSxW0eoylIsl6sxHRfhXg1no5ObdGufYhRIubP2m3FUdv-Cop3t3S_xwMbBWQ',
- 'refresh_token': 'OezXcEiiBSKSxW0eoylIeGXVsfdajniiwsVJiT7fTv7j5jCAxg',
- 'openid': u'oV02tuA8Wt6Kk7S0pVydThYvmSJA',
- 'expires_in': 7200,
- 'scope': u'snsapi_login'}
- """
- api = WeixinAPI(access_token=auth_info['access_token'])
- resp = api.user(openid=auth_info['openid'])
- # 此时的resp中已经获取到了用户的所有信息:dict类型
- # openid,nickname,sex,language,city,province,country,headimagurl
- # print(resp)
- # 校验access_token是否过期
- # v = api.validate_token(openid=auth_info['openid'])
- # print(v) # :{'errcode': 0, 'errmsg': 'ok'}
- # return jsonify(resp)
- return render_template("index.html", **resp)
-
-
- # @app.route("/login")
- # def login():
- # api = WeixinAPI(appid=APP_ID,
- # app_secret=APP_SECRET,
- # redirect_uri=REDIRECT_URI)
- # redirect_uri = api.get_authorize_login_url(scope=("snsapi_base",))
- # return redirect(redirect_uri)
-
-
- @app.route("/wx_login")
- def login():
- return render_template("wx.html")
-
-
- if __name__ == "__main__":
- app.run(debug=True)
2. wx.html代码
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title></title>
- <script src="static/jquery-1.12.4.min.js"></script>
- </head>
- <body>
- <button id="wx_login">微信登录</button>
- <script>
- $("#wx_login").click(function () {
- {#let url = encodeURIComponent(window.location.href); // 注意一定要encodeURIComponent#}
- let url = encodeURIComponent("http://127.0.0.1:5000/role"); // 注意一定要encodeURIComponent
- let url2 =
- `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxa77a00333cd8d99a&redirect_uri=${url}&response_type=code&scope=snsapi_userinfo&state=#wechat_redirect`
- window.location.href = url2
- })
- </script>
- </body>
- </html>
3. index.html代码
- <!DOCTYPE html>
- <html lang="zh-CN">
- <head>
- <meta charset="UTF-8">
- <title>微信登录成功页面</title>
- <meta name="viewport" content="width=device-width, initial-scale=1">
- </head>
- <body>
- {# {#}
- {# "city": "Tongzhou",#}
- {# "country": "CN",#}
- {# "headimgurl": "https://thirdwx.qlogo.cn/mmopen/vi_32/ibucZH2uiaQEMMzNB4M5ibQa2snJo79SibExXxLymQLrAAdO6KxXkO7qzBwGEkdOWXL4sic1DoeGut9uUShF7WUibicDA/132",#}
- {# "language": "zh_CN",#}
- {# "nickname": "M\u20f0a\u20f0y\u20f0a\u20f0n\u20f0a\u20f0n\u20f0",#}
- {# "openid": "o3uhR6v4Py3UGLtQGWzsmp9IgEfo",#}
- {# "privilege": [],#}
- {# "province": "Beijing",#}
- {# "sex": 1#}
- {#}#}
- <img src="{{ headimgurl }}" alt="头像"> <br>
- 国家:{{ country }}<br>
- 省份:{{ province }}<br>
- 城市:{{ city }}<br>
- 昵称:{{ nickname }}<br>
- 性别:{% if sex == 1 %}男{% elif sex == 0 %}女{% else %}未知{% endif %}
- </body>
- </html>
3. 依赖包需要下载下来放到环境中的site-packages里面
下载地址:https://github.com/mayanan-python/weixin-login
4. 微信公众平台链接:
http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login
5. 此处需要关注:
6. 此处需要设置项目的根url:
此处写域名和端口号
7. 项目依赖
- flask
- simplejson
- requests
- chardet
- six
- xmltodict
- pycryptodome
- requests_pkcs12