[英]How do I extract code from redirected url? (OAuth 2.0, Flask)
So the authorization request url is correct and it successfully redirects to redirect_url
which is /loginAuthorized
.所以授权请求 url 是正确的,它成功
redirect_url
到/loginAuthorized
的 redirect_url 。 I can plainly see that a code
is added as in我可以清楚地看到添加了一个
code
,如
http://127.0.0.1:5000/loginAuthorized/?code=SOME_CODE`
but can't grab it on the code.但不能在代码上抓住它。 When I print
path
, it just prints loginAuthorized/
wihtout code
.当我打印
path
时,它只打印loginAuthorized/
没有code
。 I can ask users to manually copy paste code from url, but it will not be convenient so want to avoid asking that.我可以要求用户手动从 url 复制粘贴代码,但这并不方便,所以想避免这样问。
import re
import requests
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def main():
# encoded url with information needed to request an authorization
url = "https://kauth.kakao.com/oauth/authorize?client_id=MY_CLIENT_ID&redirect_uri=http%3A%2F%2F127.0.0.1%3A5000%2F%2FloginAuthorized/&response_type=code"
return render_template('index.html', request_url=url)
# If you authorize the service, it redirected to this url
# Catch any url that has /loginAuthorized as its base
@app.route("/loginAuthorized", defaults={"path": ""})
@app.route("/<path:path>")
def loginAuthorized(path):
print('a')
print(path) # "loginAuthorized/" is printed, there is no code
print('b')
if __name__ == "__main__":
app.run()
You can use request
flask module to get query params from URL您可以使用
request
flask 模块从 URL 获取查询参数
Import request
using导入
request
使用
from flask import request
Then to get code query param from URL use.然后从 URL 使用获取代码查询参数。
request.args.get('code')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.