错误:redirect_uri_mismatch :: redirect_uri必须匹配已注册的回调URL

时间:2018-03-31 21:29:17

标签: python-2.7 flask oauth github-api

所以我正在尝试使用Github OAuth API在Flask中学习OAuth,我正在使用flask_dance库。我可以在Github上验证自己并返回我的应用程序,但是在显示404 Not Found Error并在其http://localhost:5000/login/authorized?error=redirect_uri_mismatch&error_description=The+redirect_uri+MUST+match+the+registered+callback+URL+for+this+application...的网址中,回调网址似乎正确,因为它是我在github上提供但仍然是显示redirect_uri mismatch。 我正在阅读文档here,并且知道我们可以从请求中删除redirect_uri参数,但我不知道该怎么做。 (我在Windows 10上)
你能帮忙的话,我会很高兴。感谢。

shown here

App.py

from flask import Flask, redirect, url_for
from werkzeug.contrib.fixers import ProxyFix
from flask_dance.contrib.github import make_github_blueprint, github

app = Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app)
app.secret_key = "supersekrit"
blueprint = make_github_blueprint(
    client_id="xxxxxxxxx",
    client_secret="xxxxxxxx",
)
app.register_blueprint(blueprint, url_prefix="/login")

@app.route("/signup")
def index():
    if not github.authorized:
        return redirect(url_for("github.login"))
    resp = github.get("/user")
    assert resp.ok
    return "You are @{login} on GitHub".format(login=resp.json()["login"])

if __name__ == "__main__":
    app.run(debug=True)

1 个答案:

答案 0 :(得分:2)

您的回调网址不正确 - 应为http://localhost:5000/login/github/authorized

enter image description here

Flask-Dance的文档说该代码创建了一个蓝图“github”,其中包含两个视图“/ github”和“/ github / authorized”。该蓝图还配置了url_prefix“/ login”,因此您的回调网址必须为http://localhost:5000/login/github/authorized

  

此代码生成一个实现必要视图的蓝图   OAuth舞蹈中的消费者。蓝图有两个视图:/ github,   这是用户访问以开始OAuth舞蹈的视图,以及   / github / authorized,这是用户被重定向到的视图   在OAuth舞蹈结束时。因为我们将url_prefix设置为   / login,最终结果是视图位于/ login / github和   /登录/ github上/授权。第二种观点是“授权回调   URL“您必须在创建应用程序时告诉GitHub。

相关问题