【问题标题】:Google OAuth2 Integration - Invalid parameter value for redirect_uri: Missing schemeGoogle OAuth2 集成 - redirect_uri 的参数值无效:缺少方案
【发布时间】:2021-04-03 03:59:11
【问题描述】:

尽管我在 Stackoverflow 上阅读了许多重复的问题,但我仍然无法终生弄清楚我做错了什么。

问题:我成功接收到授权码,但是当我使用此码请求访问令牌时,出现以下错误:

{
  "error": "invalid_request",
  "error_description": "Invalid parameter value for redirect_uri: Missing scheme: http%3A%2F%2Flocalhost%3A3030%2Fgoogle%2Foauth2%2Fcallback"
}

配置:

  • 我使用 http://localhost:3030/google/oauth2/callback 作为回调 URL

  • 在谷歌开发者控制台中设置:

  • 这是我发送以获取令牌的“原始 curl”请求:

    curl --location --request POST 'https://oauth2.googleapis.com/token' \
    --header 'Content-Type: application/x-www-form-urlencoded' \
    --data-urlencode 'code=4%2F0AY0e-g6zyewnsWjPEXoxZWawsp1E634ZlefYoBeYO1nXxBwjPQNCGVf7SGb4MxfNcjUApw' \
    --data-urlencode 'redirect_uri=http%3A%2F%2Flocalhost%3A3030%2Fgoogle%2Foauth2%2Fcallback' \
    --data-urlencode 'client_id=....' \
    --data-urlencode 'client_secret=....' \
    --data-urlencode 'grant_type=authorization_code'
    

    附:如您所见,我“UrlEncoded”redirect_url 以及code 因为它确实包含斜杠。为了站在同一边,我尝试对client_idclient_secretgrant_type 进行编码,但由于它们只包含 ASCII 字符,所以它们的输出相同。

我做了什么:

  • 在 SO 上通过类似问题进行研究:jenkins issueios issuephp issuemissing http issuenodejs issue - similar to this one 随后是discussionthisthat 以及所有其他出现在这里的问题- 为简洁起见将省略它们。

  • 我试过设置

    • http://localhost/google/oauth2/callback:3030 以及
    • http://127.0.0.1:3030/google/oauth2/callback
    • http://127.0.0.1/google/oauth2/callback:3000(虽然最后指定一个端口非常奇怪,将 localhost 更改为 127.0.0.1,但在其中一个类似线程中提出了建议),这些都不起作用。
  • 阅读所有文档from google

  • 在 OAuth2 Playground 上玩过(它显然可以工作),但对我不起作用

  • 尝试了 body + 不同内容类型的多种变体同样的问题,但有时我也得到了

    {
      "error": "invalid_grant",
      "error_description": "Bad Request"
    }
    

任何帮助将不胜感激。

【问题讨论】:

    标签: oauth-2.0 google-oauth


    【解决方案1】:

    一段时间后,我能够成功获得令牌。原来我没有正确地向 Google API 发出请求。此外,对于“curl”请求,它应该是--data 而不是--data-urlencode。以下请求对我有用:

    curl --request POST \
      --url https://oauth2.googleapis.com/token \
      --header 'Content-Type: application/x-www-form-urlencoded' \
      --data code=4%2F0AY0e-g4TLGE7c7VyMe8-95baQzeh0uERiKuGnHG5Sqccb4MCsmJOzV_a2jSbI9bm62VZ6Q \
      --data redirect_uri=http%3A%2F%2Flocalhost%3A3030%2Fgoogle%2Foauth2%2Fcallback \
      --data client_id=********* \
      --data client_secret=********* \
      --data grant_type=authorization_code
    

    curl --request POST \
      --url https://oauth2.googleapis.com/token \
      --header 'Content-Type: application/x-www-form-urlencoded' \
      --data 'code=4%2F0AY0e-g4TLGE7c7VyMe8-95baQzeh0uERiKuGnHG5Sqccb4MCsmJOzV_a2jSbI9bm62VZ6Q&redirect_uri=http%3A%2F%2Flocalhost%3A3030%2Fgoogle%2Foauth2%2Fcallback&client_id=*********&client_secret=*********&grant_type=authorization_code'
    

    另外一个观察:当你测试时,你只能使用一次授权码(出于安全原因)。有时即使您使用相同的代码发送多个“不成功的请求”,Google 的 API 也会拒绝使用该代码的所有后续请求(您需要再次通过 OAuth2 流程来获取新的请求)。让我感到困惑的最“令人沮丧”的部分是错误代码的响应如下所示:

    {
      "error": "invalid_grant",
      "error_description": "Bad Request"
    }
    

    而不是像“代码无效”或“代码已过期”之类的东西。

    因此,如果您遇到上述错误,则表示请求已正确制作,但代码错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-16
      • 2020-04-24
      • 2017-08-27
      • 1970-01-01
      • 1970-01-01
      • 2016-12-23
      • 2015-02-22
      • 1970-01-01
      相关资源
      最近更新 更多