NodeJS Invalid_grant gaxios.js:85:23

dan*_*ina 3 node.js google-api-nodejs-client

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

(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.
Run Code Online (Sandbox Code Playgroud)

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

小智 6

我遇到了完全相同的问题,我可以通过在oauthplayground中重新生成刷新令牌来解决此问题。具体来说,我执行了本指南中的步骤 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

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

干杯

  • 您好,我的刷新令牌在一段时间后不断过期,有没有任何解决方案可以让它不会过期,并且我可以向我的网络应用程序的所有客户发送电子邮件? (4认同)