准备
在 https://github.com/settings/developers 注册授权应用 获取appid appsecret 设置callbackurl
Application name -----------------应用名
Homepage URL -----------------主页URL,这里我填写的是本地测试的URL地址
Application description -----------------应用描述
Authorization callback URL -----------------后端回调URL
即自己实现的逻辑 主要是获取code 使用appid appsecret 获取auth_token 再使用auth_token获取用户信息 内部逻辑处理
流程
1,首先使用申请好的appid和设置好的callbackurl 去请求github授权url
https://github.com/login/oauth/authorize?client_id=4520e5d3f7afa74b26c0&redirect_uri=http://localhost:5000/customer/github/redirect
2,跳转到github登录界面 登录授权后重定向到回调url http://localhost:5000/customer/github/redirect github会返回code参数
http://localhost:5000/customer/github/redirect?code=765a0b9808771482bcc0
3,执行回调url自己实现的逻辑,即http://localhost:5000/customer/github/redirect?code=765a0b9808771482bcc0路由
3-1,使用appid和appsecret以及code参数获取auth_token,
https://github.com/login/oauth/access_token?client_id=4520e5d3f7afa74b26c0&client_secret=fbfde7bd7af8a98c1d3ffceed80294dff41613b9&code=765a0b9808771482bcc0
3-2,github返回auth_token
返回body为json,取access_token字段
3-3 拿着 auth_token 获取用户信息
用户url: https://api.github.com/user
token放到请求头
headers = {
'accept':'application/json',
'Authorization': access_token
}
3-4,拿到用户信息处理自己的逻辑,缓存redis 存数据库 登录逻辑等
测试代码
目录
project
- templates
- home.html
- login.html
main.py
main.py
# github授权登录
from flaskimport Flask
from flaskimport render_template, request
import requests
app = Flask(__name__)
client_id ="4520e5d3f7afa74b26c0"
client_secret ="fbfde7bd7af8a98c1d3ffceed80294dff41613b9"
global access_token
@app.route('/')
def github_user():
print('route /')
return render_template('home.html')
@app.route('/login')
def hello_world():
print('route /login')
return render_template('login.html')
@app.route('/customer/github/redirect')
def github_redirect():
print('route /customer/github/redirect')
global access_token
code = request.args.get('code')
print('code=', code)
token_url ="https://github.com/login/oauth/access_token?" \
'client_id={}&client_secret={}&code={}'
token_url = token_url.format(client_id, client_secret, code)
print(1111, token_url)
header = {
"accept":"application/json"
}
res = requests.post(token_url, headers=header)
if res.status_code ==200:
res_dict = res.json()
print(res_dict)
access_token = res_dict["access_token"]
user_url ='https://api.github.com/user'
access_token ='token {}'.format(access_token)
headers = {
'accept':'application/json',
'Authorization': access_token
}
isLogin =0
res = requests.get(user_url, headers=headers)
if res.status_code ==200:
user_info = res.json()
print(user_info)
email = user_info.get('email', None)
company_name = user_info.get('company',None)
print('email:', email)
print('company:', company_name)
isLogin =1
return render_template('home.html', email=email, company_name=company_name, isLogin=isLogin)
return None
if __name__ =='__main__':
app.run(debug=True,host='0.0.0.0',port=5000)
home.html
<!DOCTYPE html>
<meta charset="utf-8"/>
<title>Flask DEMO
<link rel="stylesheet" type="text/css" href="static/css/index.css"/>
<div class="header">
{% if isLogin == 1 %}
<h1>Welcome ,{{email}}!
<h1>Welcome ,{{company_name}}!
{%else%}
<h1>click here to login
<a href="http://127.0.0.1:5000/login">登录
{%endif%}
</html>
login.html
<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<title>Title
<a href="https://github.com/login/oauth/authorize?client_id=4520e5d3f7afa74b26c0&redirect_uri=http://localhost:5000/customer/github/redirect">使用GitHub账号登录
参考
https://blog.csdn.net/kobe24lmlps/article/details/80838329
https://blog.csdn.net/qq_31442743/article/details/110436864
网友评论