烧瓶舞蹈和错误:redirect_uri_mismatch

时间:2019-01-20 15:59:32

标签: python-3.x flask oauth-2.0 flask-dance

我正在遵循文档中的example

from flask import Flask, redirect, url_for
from flask_dance.contrib.google import make_google_blueprint, google

app = Flask(__name__)
app.secret_key = "supersekrit"
blueprint = make_google_blueprint(
    client_id="my-key-here",
    client_secret="my-secret-here",
    scope=[
        "https://www.googleapis.com/auth/plus.me",
        "https://www.googleapis.com/auth/userinfo.email",
    ]
)
app.register_blueprint(blueprint, url_prefix="/login")

@app.route("/")
def index():
    if not google.authorized:
        return redirect(url_for("google.login"))
    resp = google.get("/oauth2/v2/userinfo")
    assert resp.ok, resp.text
    return "You are {email} on Google".format(email=resp.json()["email"])

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

我已在Google开发人员控制台中将Web客户端应用程序配置为仅接受使用https://www.example.com/login/google/authorized端点的HTTPS。

在尝试启动整个身份验证过程后,我得到了:

Error: redirect_uri_mismatch

我在请求中看到Flask-Dance正在发送http://www.example.com/login/google/authorized(使用HTTP,而不是HTTPS)。有没有办法告诉Flask-Dance使用HTTPS?我也为HTTPS配置了我的开发环境。

1 个答案:

答案 0 :(得分:1)

如果Flask-Dance使用HTTP生成重定向URL,则意味着Flask(不是Flask-Dance)认为传入请求正在使用HTTP。 (检查request.scheme进行确认。)如果传入请求实际上是使用HTTPS,则Flask在某个地方会感到困惑,主要是由于代理。检查the Flask docs on proxy setups了解更多信息。

一旦Flask知道传入的请求正在使用HTTPS,则Flask-Dance将自动理解重定向URL也应使用HTTPS。

(来源:我是Flask-Dance的作者)