【问题标题】:Flask-Dance and Error: redirect_uri_mismatch烧瓶舞和错误:redirect_uri_mismatch
【发布时间】:2019-01-20 15:59:31
【问题描述】:

我正在关注文档中的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。

【问题讨论】:

  • 你最终做了什么来完成这项工作?
  • @stasdeep 我最终使用了OAUTHLIB_INSECURE_TRANSPORTOAUTHLIB_RELAX_TOKEN_SCOPE'1'

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


【解决方案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的作者)

【讨论】:

    猜你喜欢
    • 2018-10-18
    • 2019-01-04
    • 1970-01-01
    • 1970-01-01
    • 2021-04-29
    • 2013-08-19
    • 2011-04-10
    • 2012-12-03
    • 1970-01-01
    相关资源
    最近更新 更多