首页 文章

Google OAuth令牌交换会返回invalid_code

提问于
浏览
13

我一直在实施Google Web服务器OAuth流程,但是当我尝试使用访问令牌交换授权代码时,它总是会抱怨“invalid_code” .

这是问题:

Step 1:

将我们的一个页面重定向到'https://accounts.google.com/o/oauth2/auth?scope=email&redirect_uri=https%3A%2F%2Fmyurl.com%2Fcallback&response_type=code&client_id=some_client_id'

Step 2:

重定向发生,谷歌将重定向到我们的网址https://myurl.com/callback?code=somecode

Step 3:

curl -X POST --data“code = somecode&client_id = some_client_id&some_client_secret = some_client_secret&redirect_uri = https://myurl.com/callback&grant_type=authorization_codehttps://accounts.google.com/o/oauth2/token -v --trace-ascii / dev / stout

响应回来了:

HTTP 400错误请求

{“error”:“invalid_grant”,“error_description”:“代码无效 . ” }

有人可以帮我解决这个问题吗?谢谢!

2 回答

  • 1

    授权码的使用寿命仅为10分钟,并且只能使用一次 . 这些检查也是如此:

    • 你是否在10分钟后使用它?如果是这样,请在10分钟内使用 .

    • 你以前用过吗?如果是这样,获取一个新的,然后使用它 .

    • 服务器时间是否与Google OAuth服务器同步?如果没有,请改变你的时间 .

  • 14

    我正在使用http://localhost:8080作为我的重定向网址,因为我只是在尝试他们的示例 . 我的json文件内容有:

    "redirect_uris": [
      "http://localhost:8080"
    ],
    "javascript_origins": [
      "http://localhost:8080"
    ]
    

    在开发人员控制台中,我将redirect_uri设置为“http://localhost:8080 " and I was getting the same error. I changed it to " http://localhost:8080/”,然后它开始工作 . (最后基本上添加'/' . )

    希望这可以帮助!

相关问题