简体   繁体   English

如何从重定向的 url 中提取代码? (OAuth 2.0,烧瓶)

[英]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.

相关问题 Auflamatic Flask Oauth2如何从URL隐藏提供者代码? - Authomatic Flask Oauth2 how to hide provider code from url? Tweepy OAuth 2.0 授权代码流程与 PKCE 本机应用程序 - 我将什么值用于回调 URI/重定向 URL 和网站 ZE6B391A8D2C4D45902A23A8B658? - Tweepy OAuth 2.0 Authorization Code Flow with PKCE Native App - what values do I use for Callback URI/Redirect URL & Website URL? 如何在本地Python代码中打开Goog​​le OAuth 2.0许可屏幕? - How do I open a Google OAuth 2.0 consent screen in a local Python code? 如何在Flask中使用url_for中的变量? - How do I use variables from url_for in Flask? 如何从字符串中提取网址数据 - how do I extract the Url data from my string 如何使用RESTEasy在Google App Engine中实现oAuth 2.0? - How do I implement oAuth 2.0 in Google App Engine with RESTEasy? 如何在不使用请求模块和通过 xpath 的情况下使用 Python 提取重定向的 url? - How can I extract redirected url with Python without using requests module and via xpath? 如何使用flask.url_for()和flask-restful? - How do I use flask.url_for() with flask-restful? Flask - 在 Discord OAuth 中重定向时出现 404 错误 - Flask - 404 Error when redirected in Discord OAuth 如何从短网址或重定向网址中获取真实(最终)网址? (用于使用 python 进行抓取) - How can I get the real(final) URL from shorthen or redirected url? (for scraping using python)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM