【问题标题】:Redirect URI mismatch error from Google OAuth来自 Google OAuth 的重定向 URI 不匹配错误
【发布时间】:2021-12-10 07:57:20
【问题描述】:

我有一个托管在 Google Cloud Run 中的 Flask Web 应用程序,该应用程序由 https://mydomain.run.app 托管。

现在我正在尝试向其添加 google 身份验证。我在 GCP 的 credentials 下创建了 API。我在重定向 uri 中给出了https://mydomain.run.app/authorize,但是当我尝试从我的应用程序登录时,它会抛出重定向不匹配错误。 错误显示我http://mydomain.run.app/authorize。 不匹配的是 httpshttp 当我尝试在凭据 uri 中提供 http 时,它会抛出我

Invalid Redirect: This app has a publishing status of "In production". URI must use https:// as the scheme.

@app.route('/login/google')
def google_login():
    google = oauth.create_client('google')
    redirect_uri = url_for('authorize', _external=True,_scheme='https')
    return google.authorize_redirect(redirect_uri)

@app.route('/authorize')
def authorize():
    google = oauth.create_client('google')  
    token = google.authorize_access_token()  
    resp = google.get('userinfo')  
    user_info = resp.json()
    user = oauth.google.userinfo() 
    session['profile'] = user_info
    session.permanent = True  
    return redirect('/select')

【问题讨论】:

  • 您的代码在调用 Google 身份验证端点时指定了 http 方案。
  • 您可以建议任何 https 方案的参考或示例吗?
  • 您在寻找什么参考资料?您的代码造成了问题。
  • 我已将上面的代码添加到我的帖子中。我无法弄清楚我在哪里犯错。我是新来的。谢谢!

标签: flask google-cloud-platform google-oauth google-authentication redirect-uri-mismatch


【解决方案1】:

授权的重定向 URIs

你应该多放 1 个 URI:

https://mydomain.run.app/

然后再次检查。我之前也遇到过同样的问题。

【讨论】:

  • 谢谢!但没有运气。它仍然显示与 http 链接相同的重定向不匹配 URI 错误。知道可能出了什么问题吗?
  • 啊,退货时请检查你的应用,应该是用https退货
  • 应用只有在暴露时才能在 https 上运行
  • 我尝试在重定向时在 url_for 中添加 _scheme='https' 但现在我是 authlib.integrations.base_client.errors.MismatchingStateError: mismatching_state: CSRF 警告!请求和响应中的状态不相等。 错误。我已将我的密钥更改为静态字符串而不是随机数。但同样的错误。
【解决方案2】:

您的应用目前已在 Google 开发者控制台中设置为生产环境。

这意味着您尝试添加到项目中的所有重定向 uri。必须是 HTTPS 而不是 HTTP 你也不能使用 localhost

当您尝试使用 http://mydomain.run.app/authorize 时,您需要将其更改为 https://mydomain.run.app/authorize 请注意第一个是 http:// 而不是 https://

出现错误是因为您的应用程序本身正在尝试发送 http 而不是 https 的重定向 uri。您需要修复您的应用程序,以便它使用 https。

【讨论】:

  • 我尝试在重定向时在 url_for 中添加 _scheme='https' 但现在我是 authlib.integrations.base_client.errors.MismatchingStateError: mismatching_state: CSRF 警告!请求和响应中的状态不相等。 错误。我已将我的密钥更改为静态字符串而不是随机数。但同样的错误。
猜你喜欢
  • 2015-08-09
  • 2017-12-10
  • 2016-01-08
  • 1970-01-01
  • 2014-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多