当前位置:网站首页>Third party login process of flask Weibo
Third party login process of flask Weibo
2022-07-29 01:24:00 【:D...】
Catalog
One 、 Third party login flowchart

Two 、Flask Code implementation
1. Configure third-party login Judge the binding and log in directly
The code is as follows ( Example ):
# Weibo three party login
class Weibo_login(Resource):
def get_weibo_user(self, code):
# Construction parameter acquisition weibo User information for
# base_url The address of the login request in the Weibo three-party login document
base_url = 'https://api.weibo.com/oauth2/access_token'
data_ = {
"client_id": "2775107052", # Assigned when applying AppKey
"client_secret": "718b38b04fc387700cc1595b30875b19", # Assigned when applying AppSecret
"grant_type": "authorization_code", # Type of request , Fill in authorization_code
"redirect_uri": "http://127.0.0.1:8080/weibo", # token url , It should be consistent with the callback address in the registration application
"code": code, # The user information ID returned to the backend after the user successfully logs in to the three parties
}
try:
# Send a request to get user information Incoming address and data
resp = requests.post(url=base_url, data=data_).json()
print("resp=========>", resp)
except Exception as e:
print(f' Failed to get Weibo user data {
e}')
return {
'code': 500, "message": 'post Failed to obtain Weibo user information '}
if not resp['access_token']:
return {
'code': 400, "message": ' according to code Failed to obtain Weibo user information '}
user_info = resp
# Return user information
return user_info
def get(self):
""" according to code Get microblog user information """
parser = reqparse.RequestParser()
parser.add_argument('code')
args = parser.parse_args()
# 1. obtain code value
# The user information ID returned to the backend after the user successfully logs in to the three parties
code = args.get('code')
print('code: The user information ID returned to the backend ', code)
# get_weibo_user The method defined above : Construction parameter acquisition weibo User information for
user_info = self.get_weibo_user(code)
print('user_info: Acquired weibo User information for ', user_info)
access_token = user_info['access_token']
# Query whether the user is bound in the relationship table of three-party login
weibo_user = OAuthUserModel.query.filter_by(oauth_id=access_token, oauth_type='weibo').first()
if not weibo_user: # Unbound Response debinding
return {
'massage': ' Unbound , Please go to bind ', 'uid': access_token, 'code': 400}
else: # binding Query user data Generate token Return response
user = Users.query.filter_by(uid=weibo_user.user).first()
payload = {
'user_id': user.uid,
'user_name': user.account,
}
# generate_token Packaged Generate token Methods
token = generate_token(payload)
if token:
return {
'code': 200, "account": user.account, 'uid': user.uid, 'token': token}
2. Unbound Binding method
# Bind microblog
class WeiboBindUser(Resource):
def post(self):
# receive data Judge whether the user exists There is Check the password Add the tripartite table through verification Generate token non-existent Response registration
parser = reqparse.RequestParser()
argss = ['account', 'password', 'unid']
for i in argss:
parser.add_argument(i)
args = parser.parse_args()
account = args['account']
password = args['password']
unid = args['unid'] # access_token
user = Users.query.filter_by(account=account).first()
if not user:
return {
'message': ' Please register first ', 'code': 400}
pwd_bool = check_password_hash(user.password, password)
if not pwd_bool:
return {
'message': ' Wrong password ', 'code': 400}
# Join the tripartite table
user_weibo = OAuthUserModel(oauth_id=unid, user=user.uid, oauth_type='weibo')
db.session.add(user_weibo)
db.session.commit()
# Generate token
payload = {
'user_id': user.uid,
'user_name': account,
}
token = generate_token(payload)
return {
'code': 200, 'account': account, 'token': token, 'uid': user.uid}
3. Guide pack
import base64
import hmac
import json
import time
import urllib
from urllib.parse import parse_qs
import requests
from flask import Blueprint, current_app
from flask_restful import Api, Resource, reqparse
from werkzeug.security import check_password_hash
from common.jwt.token_generate import generate_token
from common.models import db
from common.models.users import OAuthUserModel, Users
from common.utils.output_json import my_output_json # Custom return format The way of encapsulation
边栏推荐
- Cookies and sessions
- ActiveMQ基本详解
- Introduction to FLV documents
- redis安装,集群部署与常见调优
- [idea] where to use the query field
- Univariate function integration 1__ Indefinite integral
- 图扑软件亮相 2022 福州数博会,携手共创数字新时代
- 【SQL之降龙十八掌】01——亢龙有悔:入门10题
- [target detection] Introduction to yolor theory + practical test visdrone data set
- Transfer: cognitive subculture
猜你喜欢

Tupu software appeared at the 2022 Fuzhou digital Expo to jointly create a new digital era

Google play APK uploads other international app stores

数字孪生轨道交通:“智慧化”监控疏通城市运行痛点

面试官:程序员,请你告诉我是谁把公司面试题泄露给你的?

大页内存原理及使用设置

进程和线程知识点总结1

Classification prediction | MATLAB realizes time series classification prediction of TCN time convolution neural network

Bracket matching test

Recommended Spanish translation of Beijing passport

Google Play APK 上传其他国际应用商店
随机推荐
Django uses the existing data table method of MySQL database
Hilbert 变换与瞬时频率
Visual full link log tracking
Common functions and usage of numpy
mysql 创建索引的三种方式
Interviewer: programmer, please tell me who leaked the company interview questions to you?
C语言300行代码实现扫雷(可展开+可标记+可更改困难级别)
Return the member function of *this
时间复杂度、空间复杂度的学习总结
一文搞懂ES6基本全部语法
Self-attention neural architecture search for semantic image segmentation
Inftnews | yuanuniverse shopping experience will become a powerful tool to attract consumers
“index [hotel/jXLK5MTYTU-jO9WzJNob4w] already exists“
Day2: 130 questions in three languages
“index [hotel/jXLK5MTYTU-jO9WzJNob4w] already exists“
测试/开发程序员靠技术渡过中年危机?提升自己本身的价值......
Y80. Chapter 4 Prometheus big factory monitoring system and practice -- Kube state metrics component introduction and monitoring extension (XI)
Flink Postgres CDC
js判断 数组/对象数组 1 是否包含数组/对象数组 2
Rewriting method set