当前位置:网站首页>基于flask的三方登陆的流程
基于flask的三方登陆的流程
2022-07-31 05:09:00 【城南花开了^】
第三方登录的流程图

具体实现步骤:
- 在前端页面点击第三方的图标,进行页面的跳转
- 跳转页面有钉钉或者是QQ登录的二维码,进行登录或注册
- 返回给后端服务器用户的信息生成token,然后传参给前端浏览器
- 浏览器保存token信息,下次扫码时验证token直接登录第三方应用
举例:
前端页面的方法:
// qq登录
QQ:function(){
// var data = {} //正规的写法
// qq_login(data).then(resp=>{
// console.log('qq登录的响应',resp)
// if(resp.code==200){
// let url = resp.url
// console.log(url)
// window.location.href = url
// }
// }).catch(err=>{
// })
// https://graph.qq.com/oauth2.0/show?which=Login&display=pc&response_type=code&client_id=101474184&redirect_uri=http%3A%2F%2Fwww.meiduo.site%3A8080%2Foauth_callback.html&state=%2F&scope=get_user_info
var app_id = '101474184'
var app_key = 'c6ce949e04e12ecc909ae6a8b09b637c'
var state = '/'
// var redirect_uri = config['beasurl']+'qq'//'http://www.meiduo.site:8080/oauth_callback.html'
var redirect_uri = 'http://www.meiduo.site:8080/oauth_callback.html'
var url = 'https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id='+app_id+'&redirect_uri='
+redirect_uri+'&state='+state+'&scope=get_user_info'
window.location.href = url
flask后端的具体实现:
# 1.生成qq扫码登录的url,返回给前端用户 # 2.用户扫码,进行登录,qq对用户进行鉴权 # 3.用户在手机上进行确认登录,返回一个code,code代表的是登陆成功的信息 # 4.code 要返回给网站,网站拿到后判断是否有账号绑定 #1.绑定过账号,直接登录成功 #2.没有绑定过账号class OauthUser(Resource): def get(self): """ 生成qq扫码登录的url,返回给前端用户 :params: response_type=code # 扫码登录 :params: state, redirect_uri :return: https://graph.qq.com/oauth2.0/show?which=Login&display=pc&response_type=code&state=9B93F12415BCB93941FDA383B66278470B75111C8D2BDC15474EF49E12D42E28F4FBBFC996B3C21250B8B7D1F915AA0A&client_id=100273020&redirect_uri=https%3A%2F%2Fqq.jd.com%2Fnew%2Fqq%2Fcallback.action%3Fview%3Dnull%26uuid%3D72a93e228e5c4386a2a743091db6e5fd """ base_url = 'https://graph.qq.com/oauth2.0/authorize?' params = { 'response_type': 'code', 'client_id': current_app.config.get('QQ_APP_ID'), 'redirect_uri': current_app.config.get('QQ_REDIRECT_URL'), 'state': current_app.config.get('QQ_STATE'), 'scope': 'get_user_info' # 返回qq用户的openid } # urlencode 会把字典转换成url的查询字符串 data = urlencode(params) url = base_url + data return {'url':url,'code':200} class QQUserinfo(Resource): def code_value(self,code): base_url='https://graph.qq.com/oauth2.0/token' if code: data ={ 'grant_type':'authorization_code', 'client_id':current_app.config.get('QQ_APP_ID'), 'client_secret':current_app.config.get('QQ_APP_KEY'), 'code':code, 'redirect_uri': current_app.config.get('QQ_REDIRECT_URL'), } resp = requests.request('GET',base_url,params=data) print('>>>>',resp) token_value =resp.text.split('&')[0][3] return token_value def get_openid(self,token_v): # 获取qq的openid base_url = 'https://graph.qq.com/oauth2.0/token' data = {'access_token': token_v} resp = requests.request('GET', base_url, params=data) user_dict = json.loads(resp.text) openid = user_dict['openid'] return openid def get_qq_nickname(self,openid,token): base_url = 'https://graph.qq.com/user/get_user_info' data={ 'ouath_consumer_key':current_app.config.get('QQ_APP_ID'), 'access_token':token, 'openid':openid } resp =requests.request('GET',base_url,params=data) nick_name = json def get(self): parse = reqparse.RequestParser() parse.add_argument('code') args= parse.parse_args() code= args.get('code') print('>>>>',code) token_v= self.code_value(code) openid= self.get_openid(token_v) qq_nickname=self.get_qq_nickname(openid,token_v) return 'hhh'
边栏推荐
- SQL行列转换
- Numpy中np.meshgrid的简单用法示例
- Three oj questions on leetcode
- Kubernetes加入集群的TOKEN值过期
- Sun Wenlong, Secretary General of the Open Atom Open Source Foundation |
- .NET-6.WinForm2.NanUI learning and summary
- Moment Pool Cloud quickly installs packages such as torch-sparse and torch-geometric
- TOGAF之架构标准规范(一)
- 【MySQL8入门到精通】基础篇- Linux系统静默安装MySQL,跨版本升级
- Information System Project Manager Core Test Site (55) Configuration Manager (CMO) Work
猜你喜欢

STM32 - DMA

The monitoring of Doris study notes

MySQL transaction isolation level, rounding

Goodbye to the cumbersome Excel, mastering data analysis and processing technology depends on it

Apache DButils使用注意事项--with modifiers “public“

STM32——DMA

The MySQL database installed configuration nanny level tutorial for 8.0.29 (for example) have hands

MySQL optimization: from ten seconds to three hundred milliseconds
![[Cloud Native] DevOps (5): Integrating Harbor](/img/5a/dd33d7d3cb0124f328d2d38fff0125.png)
[Cloud Native] DevOps (5): Integrating Harbor

Mysql——字符串函数
随机推荐
MySQL transaction isolation level, rounding
centos7安装mysql5.7步骤(图解版)
关于小白安装nodejs遇到的问题(npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)
MySQL database addition, deletion, modification and query (detailed explanation of basic operation commands)
Temporal线上部署
Centos7 install mysql5.7 steps (graphical version)
ES 源码 API调用链路源码分析
【一起学Rust】Rust的Hello Rust详细解析
datagrip带参sql查询
Multiple table query of sql statement
Lock wait timeout exceeded解决方案
2022-07-30:以下go语言代码输出什么?A:[]byte{} []byte;B:[]byte{} []uint8;C:[]uint8{} []byte;D:[]uin8{} []uint8。
Sql解析转换之JSqlParse完整介绍
Linux的mysql报ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘ (using password NOYSE)
城市内涝及桥洞隧道积水在线监测系统
MySQL8.0.26安装配置教程(windows 64位)
矩池云快速安装torch-sparse、torch-geometric等包
MySQL事务隔离级别详解
DVWA安装教程(懂你的不懂·详细)
STM32 - DMA