使用 oAuth 和 youtube 进行身份验证时,在第二次身份验证尝试时总是出现错误:invalid_grant,为什么?
截至昨天,我的应用程序第一次能够通过 oAuth 2.0 与 google (youtube) 进行身份验证,没有问题,但第二次(重新身份验证,同一应用程序 + 同一用户),当我将 requestToken 交换为 accessToken 时,我得到一个错误:
error : invalid_grant
我正在使用:
grant_type = authorization_code
就像他们建议的那样。两天前发生的情况是,在重新验证时,网页会显示“您之前已经使用此应用程序进行了验证,您想再次授予其访问权限吗?”。发生了什么变化或者我可能做错了什么?
As of yesterday, my application is able to authenticate with google (youtube) via oAuth 2.0 the first time no problem but the 2nd time (re-authentication, same app + same user), when I exchange the requestToken for an accessToken I get an error:
error : invalid_grant
I'm using:
grant_type = authorization_code
like they suggest. What used to happen, before 2 days ago, was on re-authentication the web page would say 'you've already authenticated with this application before, would you like to grant it access again?'. What has changed or what could I be doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了解决这个问题的方法(至少对我们来说)。当重定向到accounts.google.com/o/oauth2/auth url时,我们必须添加approval_prompt=force和access_type=offline。如果缺少一个,我们就得不到刷新令牌。
此网址记录了所有参数,但没有提及刷新令牌:http:// /code.google.com/apis/accounts/docs/OAuth2WebServer.html
I found a way to fix this (at least for us). When redirecting to the accounts.google.com/o/oauth2/auth url, we had to add both approval_prompt=force and access_type=offline. If one was missing, we got no refresh token.
This url documents all of the parameters, but says nothing about the refresh token: http://code.google.com/apis/accounts/docs/OAuth2WebServer.html
我想我可能在这里找到了答案:
https://groups.google.com/forum/#!searchin/oauth2-dev/invalid_grant/oauth2-dev/eaOa6THER0k/z4eO-taUFxgJ
问题在于“代码”您需要发送的参数需要进行编码。当您将approval_prompt 设置为force 时它将起作用,因为返回代码包含URL 安全字符。但是,第二次授权请求时的返回代码并非如此。
对我来说这解决了这个问题,我希望它也适合你。
I think I may have found the answer here:
https://groups.google.com/forum/#!searchin/oauth2-dev/invalid_grant/oauth2-dev/eaOa6THER0k/z4eO-taUFxgJ
The problem was that that the "code" parameter that you need to send needs to be encoded. It will work when you set the approval_prompt to force because the return code contains URL-safe characters. However, this is not the case for the return code upon a second authorization request.
For me this fixed it, I hope it does for you as well.