如何修复 Google API“invalid_grant”错误

发布于 2025-01-10 14:27:45 字数 466 浏览 3 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里梦着梦中梦 2025-01-17 14:27:45

另一个潜在问题:默认凭据丢失或过期。

某些应用依赖 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.

你爱我像她 2025-01-17 14:27:45

如果您检查过期文档,您就会找到问题的原因。

为外部用户类型配置了 OAuth 同意屏幕且发布状态为“测试”的 Google Cloud Platform 项目获得了一个刷新令牌,该令牌将在 7 天后过期。

在 Google 开发者控制台中将您的应用程序设置为生产环境,您的刷新令牌的持续时间将超过 7 天。

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.

Set your app to production in Google developer console and your refresh tokens will last longer then seven days.

感情洁癖 2025-01-17 14:27:45

以下命令将修复它。

gcloud auth application-default login

The following command will fix it.

gcloud auth application-default login
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文