如何修复 Google API“invalid_grant”错误
我使用 Google API 与 Drive API 和 Drive Activity API 来处理和更改文件。 一切似乎都工作正常,但在一周中的任意一天,我收到一个 invalid_grant
错误。我读过有关时区解决方案和其他 8-9 问题的内容,但我认为这是另一回事。
这就是我使用 API 的方式:
- 从 DB 获取凭证 ->使用它们进行身份验证。
- 每次我的应用程序执行 auth 函数时都会获取新的 access_token 和 refresh_token,将令牌设置为凭据并在数据库中更新它。
- 每次用户想要对驱动器执行任何操作或通过 google webhook 接收新更改时,都会触发 auth 方法(如果存储 OAuth2Client 的 authClient 静态字段未定义),
因此它工作正常,但此 invalid_grant 错误会破坏一切。我不知道该怎么办。
I am using Google API with Drive API and Drive Activity API to handle and make changes to files.
Everything seems to work fine, but at random day of the week I receive an invalid_grant
error. I have read about timezone solutions and other 8-9 issues, but I think this is something else.
This is how I work with API:
- Get credentials from DB -> use them to auth.
- Getting new access_token and refresh_token every time my application executes the auth function, setting tokens to credentials and updating it in DB.
- Every time user wants to do anything with drive or receiving new changes via google webhook, auth method is being triggered (if authClient static field which is storing OAuth2Client is undefined)
So it works fine, but this invalid_grant error ruins everything. I have no idea what to do with this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
另一个潜在问题:默认凭据丢失或过期。
某些应用依赖 Google Cloud 默认凭据来运行。这些有时是从执行 shell 的环境变量中读取的。请注意,即使您使用
gcloud auth login
登录非默认凭据,这些仍然可能会导致问题。通过运行以下命令检查您的默认凭据令牌是否有效:
gcloud auth application-default print-access-token
请参阅本文了解更多详细信息。
Another potential problem: Missing or expired default credentials.
Some apps depend on Google Cloud default credentials to operate. These are sometimes read off of environment variables from the executing shell. Note that these can still cause problems even if you use
gcloud auth login
to log in to non-default credentials.Check that your default credential token is valid by running the following:
gcloud auth application-default print-access-token
See this article for more details.
如果您检查过期文档,您就会找到问题的原因。
在 Google 开发者控制台中将您的应用程序设置为生产环境,您的刷新令牌的持续时间将超过 7 天。
if you check the documentation for expiration you will find the cause of your issue.
Set your app to production in Google developer console and your refresh tokens will last longer then seven days.
以下命令将修复它。
The following command will fix it.