简体   繁体   English

如何修复谷歌 API 'invalid_grant' 错误

[英]How to fix Google API 'invalid_grant' error

I am using Google API with Drive API and Drive Activity API to handle and make changes to files.我正在使用 Google API 和 Drive API 和 Drive Activity API 来处理和更改文件。 Everything seems to work fine, but at random day of the week I receive an invalid_grant error.一切似乎都运行良好,但在一周中的随机某一天,我收到了一个invalid_grant错误。 I have read about timezone solutions and other 8-9 issues, but I think this is something else.我读过时区解决方案和其他 8-9 问题,但我认为这是另外一回事。

This is how I work with API:这就是我使用 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.每次我的应用程序执行 auth function 时获取新的 access_token 和 refresh_token,将令牌设置为凭据并在数据库中更新它。
  • 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)每次用户想对驱动器做任何事情或通过 google webhook 接收新的更改时,都会触发 auth 方法(如果存储 OAuth2Client 的 authClient static 字段未定义)

So it works fine, but this invalid_grant error ruins everything.所以它工作正常,但这个 invalid_grant 错误毁了一切。 I have no idea what to do with this.我不知道该怎么办。

if you check the documentation for expiration you will find the cause of your issue.如果您检查文档是否过期,您将找到问题的原因。

A Google Cloud Platform project with an OAuth consent screen configured for an external user type and a publishing status of "Testing" is issued a refresh token expiring in 7 days.为外部用户类型配置了 OAuth 同意屏幕且发布状态为“测试”的 Google Cloud Platform 项目获得了 7 天后到期的刷新令牌。

Set your app to production in Google developer console and your refresh tokens will last longer then seven days.在 Google 开发者控制台中将您的应用程序设置为生产环境,您的刷新令牌将持续超过 7 天。

Another potential problem: Missing or expired default credentials.另一个潜在问题:缺少或过期的默认凭据。

Some apps depend on Google Cloud default credentials to operate.某些应用依赖于 Google Cloud 默认凭据来运行。 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.这些有时会从执行 shell 的环境变量中读取。请注意,即使您使用gcloud auth login非默认凭据,这些仍然会导致问题。

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.有关更多详细信息,请参阅本文

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM