首页 > 解决方案 > NodeJS Invalid_grant gaxios.js:85:23

问题描述

在我的 Node.js 应用程序中,我使用 googleapis 和 oauth2 身份验证来发送电子邮件。突然,我在启动应用程序时发现了这个错误。

(node:1333) UnhandledPromiseRejectionWarning: Error: invalid_grant
    at Gaxios._request (/Users/danielefarina/Desktop/GIT/server-worldmarker/src/node_modules/gaxios/build/src/gaxios.js:85:23)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async OAuth2Client.refreshTokenNoCache (/Users/danielefarina/Desktop/GIT/server-worldmarker/src/node_modules/google-auth-library/build/src/auth/oauth2client.js:170:21)
    at async OAuth2Client.refreshAccessTokenAsync (/Users/danielefarina/Desktop/GIT/server-worldmarker/src/node_modules/google-auth-library/build/src/auth/oauth2client.js:194:19)
    at async OAuth2Client.getAccessTokenAsync (/Users/danielefarina/Desktop/GIT/server-worldmarker/src/node_modules/google-auth-library/build/src/auth/oauth2client.js:214:23)
(node:1333) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:1333) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

您对如何解决有任何想法吗?

标签: node.jsgoogle-api-nodejs-client

解决方案


我遇到了完全相同的问题,我可以通过在oauthplayground中重新生成 refresh_token 来解决这个问题。具体来说,我执行了本指南中的第 4 步,以获得一个新的刷新令牌,我在我的应用程序中替换了它,并使用现有的 client_id 和 secret_id 一切继续按预期工作。

我不是 oauth 专家,所以我无法完全解释为什么会发生这种情况,但是在做了一些研究之后,我发现刷新令牌确实过期(我认为它们没有过期),并且出于某种原因我的刷新令牌,这可能是一个月大,突然停止工作。

这是一篇很好的文章,对刷新令牌进行了一些解释:https ://medium.com/@bantic/more-oauth-2-0-surprises-the-refresh-token-1831d71f4af6 。

在这里你可以找到关于刷新令牌过期的官方解释:https ://developers.google.com/identity/protocols/oauth2#expiration

我希望这可以帮助您解决问题。

干杯


推荐阅读