【问题标题】:Google OAuth 2 authorization - Error: Redirect Uri MismatchGoogle OAuth 2 授权 - 错误:重定向 Uri 不匹配
【发布时间】:2015-08-09 14:41:03
【问题描述】:

JSON 文件

{"web":{"auth_uri":"https://accounts.google.com/o/oauth2/auth",
    "client_secret":"c-kaafSexciO7It3QcKxx3BO",
    "token_uri":"https://accounts.google.com/o/oauth2/token",
    "client_email":"xxx678964-tjkl572knihtgocll9tnadvsdngmnld6@developer.gserviceaccount.com",
    **"redirect_uris":["http://www.alfrosia.com"]**,


"client_x509_cert_url":"https://www.googleapis.com/robot/v1/metadata/x509/798911678964-tjkl572knihtgocll9tnadvsdngmnld6@developer.gserviceaccount.com",
        "client_id":"xxx1678964-tjkl572knihtgocll9tnadvsdngmnld6.apps.googleusercontent.com",    
"auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs",
        "javascript_origins":["[http://www.alfrosia.com][1]"]}}

我在上面的 json 文件中指定了正确的重定向 url,但是在身份验证期间它在浏览器上给出了错误的 url 消息。虽然我已经给出,this 作为 json 文件中的重定向 Uri。

在错误消息中它说

redirect_uri=http://localhost:57826/authorize/

是 uri_mismatch 而我没有在 json 中指定它

但是 Google OAuth2 授权失败,在认证过程中出现此错误消息

- Error: redirect_uri_mismatch.

请求详情

scope=https:[//www.googleapis.com/auth/youtube.readonly][3]
response_type=code
redirect_uri=http://localhost:57826/authorize/ 
access_type=offline
pageId=none
client_id=xxxxx964-tjkl572knihtgocll9tnadvsdngmnld6.apps.googleuserconten

【问题讨论】:

    标签: google-api google-oauth


    【解决方案1】:

    “localhost”在 Google 开发者控制台中不起作用。

    尝试使用 127.0.0.1 作为重定向 URI,并且不要在 URI 中使用符号。

    【讨论】:

      【解决方案2】:

      redirect_uri 必须与您在Google Developer console 中设置的完全匹配。我的猜测是您的代码/客户端库/IDE 会根据您浏览的 url 自动设置重定向 URI。

      例如,Visual Studio 有随机更改端口的习惯。

      选项 1:

      将您在 Google 开发者控制台中的重定向 uri 修复为

      http://localhost:57826/authorize/

      选项 2:

      假设您正在使用 Visual Studio 修复项目设置,使其停止添加随机端口。

      选项 3:

      假设您正在使用可能添加此随机端口的 java 或 Visual Studio IDE。创建 Client ID for native application 而不是 Client ID for web application 并将其用于在本地主机上进行测试。不要将Client ID for native application 发布到您的实时网络服务器。

      【讨论】:

      • 如果我们把重定向作为本地主机。它甚至适用于已发布的网站。我的意思是对于已发布的网站,我们需要提供实际的重定向,对吗?或者这个本地主机会工作吗?
      • 在这种情况下,您也可以只使用不需要刷新 uri 的本机 oauth2 代码。但是本地主机也应该可以工作
      猜你喜欢
      • 2020-10-15
      • 2021-12-10
      • 2017-11-28
      • 2017-12-10
      • 1970-01-01
      • 1970-01-01
      • 2019-05-09
      • 1970-01-01
      • 2012-07-14
      相关资源
      最近更新 更多