当前位置:网站首页>基于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'
边栏推荐
- Error EPERM operation not permitted, mkdir 'Dsoftwarenodejsnode_cache_cacach Two solutions
- Information System Project Manager Core Test Site (55) Configuration Manager (CMO) Work
- 信息系统项目管理师核心考点(五十五)配置管理员(CMO)的工作
- 太厉害了,终于有人能把文件上传漏洞讲的明明白白了
- 【ORACLE Explain 详解】
- 如何将项目部署到服务器上(全套教程)
- MySQL开窗函数
- MySQL优化之慢日志查询
- MySQL优化:从十几秒优化到三百毫秒
- Input length must be multiple of 8 when decrypting with padded cipher
猜你喜欢

MySQL常见面试题汇总(建议收藏!!!)

About the problems encountered by Xiaobai installing nodejs (npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)
【一起学Rust】Rust的Hello Rust详细解析

centos7安装mysql5.7

ERROR 1819 (HY000) Your password does not satisfy the current policy requirements

Unity mobile game performance optimization series: performance tuning for the CPU side

Sun Wenlong, Secretary General of the Open Atom Open Source Foundation |

面试Redis 高可靠性|主从模式、哨兵模式、Cluster集群模式

关于小白安装nodejs遇到的问题(npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)

Multiple table query of sql statement
随机推荐
Moment Pool Cloud quickly installs packages such as torch-sparse and torch-geometric
On-line monitoring system for urban waterlogging and water accumulation in bridges and tunnels
ERP Production Operation Control Kingdee
CentOS7 —— yum安装mysql
12个MySQL慢查询的原因分析
Temporal线上部署
SQL statement to range query time field
Unity Framework Design Series: How Unity Designs Network Frameworks
MySQL优化之慢日志查询
centos7安装mysql5.7步骤(图解版)
ABC D - Distinct Trio(k元组的个数
精解四大集合框架:List 核心知识总结
Distributed Transactions - Introduction to Distributed Transactions, Distributed Transaction Framework Seata (AT Mode, Tcc Mode, Tcc Vs AT), Distributed Transactions - MQ
MySQL8.0安装教程,在Linux环境安装MySQL8.0教程,最新教程 超详细
Kubernetes加入集群的TOKEN值过期
110道 MySQL面试题及答案 (持续更新)
Redis Advanced - Cache Issues: Consistency, Penetration, Penetration, Avalanche, Pollution, etc.
Doris学习笔记之监控
太厉害了,终于有人能把文件上传漏洞讲的明明白白了
a different object with the same identifier value was already associated with the session