当前位置:网站首页>基于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'
边栏推荐
- ABC D - Distinct Trio (Number of k-tuples
- ERROR 2003 (HY000) Can't connect to MySQL server on 'localhost3306' (10061)
- mysql存储过程
- mysql使用on duplicate key update批量更新数据
- Sun Wenlong, Secretary General of the Open Atom Open Source Foundation |
- 【MySQL8入门到精通】基础篇- Linux系统静默安装MySQL,跨版本升级
- MySQL8.0.26安装配置教程(windows 64位)
- CentOS7 install MySQL graphic detailed tutorial
- MySQL transaction (transaction) (this is enough..)
- SQL row-column conversion
猜你喜欢
城市内涝及桥洞隧道积水在线监测系统
centos7安装mysql5.7步骤(图解版)
一文了解大厂的DDD领域驱动设计
MySQL optimization slow log query
matlab simulink欠驱动水面船舶航迹自抗扰控制研究
Interview | Cheng Li, CTO of Alibaba: Cloud + open source together form a credible foundation for the digital world
Interview Redis High Reliability | Master-Slave Mode, Sentinel Mode, Cluster Cluster Mode
限流的原理
Refinement of the four major collection frameworks: Summary of List core knowledge
Redis进阶 - 缓存问题:一致性、穿击、穿透、雪崩、污染等.
随机推荐
分布式事务处理方案大 PK!
centos7安装mysql5.7
DVWA靶场环境搭建
面试Redis 高可靠性|主从模式、哨兵模式、Cluster集群模式
Paginate the list collection and display the data on the page
【ORACLE Explain 详解】
Typec手机有线网卡网线转网口转接口快充方案
The monitoring of Doris study notes
限流的原理
MySQL8.0.26安装配置教程(windows 64位)
Goodbye to the cumbersome Excel, mastering data analysis and processing technology depends on it
[Detailed explanation of ORACLE Explain]
Lock wait timeout exceeded解决方案
Flink sink ES 写入 ES(带密码)
a different object with the same identifier value was already associated with the session
The 15th day of the special assault version of the sword offer
Pytorch教程Introduction中的神经网络实现示例
Shell重油常压塔模拟仿真与控制
Summary of MySQL common interview questions (recommended collection!!!)
MYSQL一站式学习,看完即学完