关于 OAuth2.0 授权套路

长话短说,一般OAuth2.0授权生成API调用需要传入header的token我们需要做两步操作:
1.请求code信息
2.根据code换token
主要流程,一般在相关应用的开发者平台申请一个应用得到第三方调用的账户信息:
Client ID:此处省略10086字符
Client Secret:**
在申请应用的时候会让你指定访问主页及回调地址,下面以语雀为例,通过建立应用,本地用python构建获取token的完整流程:
file
上面我将主页设置为:127.0.0.1:5000
将回调地址设置为:127.0.0.1:5000/yuque/callback
参考语雀开发文档:

https://www.yuque.com/yuque/developer

授权流程:

https://www.yuque.com/yuque/developer/authorizing-oauth-apps

然后本地python脚本代码如下:

from flask import Flask,redirect,request,json
from furl import furl
import requests

client_id='你的id'
client_secret='你的secret'
app = Flask(__name__)
@app.route('/',methods=['GET','POST'])
def fx():
    url='https://www.yuque.com/oauth2/authorize'
    params={'client_id':client_id,'scope':'group,book,design,sheet,doc,repo','state':'123456','response_type':'code'}
    url=furl(url).set(params)
    return redirect(url,302)

#回调路由
@app.route('/yuque/callback')
def fx2():
    code=request.args.get('code')
    access_token_url='https://www.yuque.com/oauth2/token'
    payload={'client_id':client_id,'client_secret':client_secret,'code':code,'grant_type':'authorization_code'}
    r=requests.post(access_token_url,json=payload,headers={'Accept':'application/json'})
    return json.loads(r.text).get('access_token')
if __name__ == '__main__':
    app.run()

双击上面的脚本文件启动http服务,不要关掉黑命令框,然后你在浏览器网址栏输入127.0.0.1:5000
回车,如果你没有在浏览器登陆账户,会让你先登陆然后确认授权,如果账号是登陆状态则直接跳转到授权确认界面,点击授权确认后网页即可得到token!
file
file
file
用上面拿到的token调用API试试水:
file

道高一尺 魔高一丈
https://pbihub.cn/users/44
M与DAX的恩怨纠葛