NodeJS Invalid_grant gaxios.js:85:23

问题描述 投票:0回答:2

在我的 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.

您有解决办法吗?

node.js google-api-nodejs-client
2个回答
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

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

干杯


0
投票

就我而言,我在使用

serverless deploy
时遇到此错误,所以我执行了以下操作:

gcloud login
gcloud auth application-default login
cp ~/.config/gcloud/application_default_credentials.json ~/.gcloud/keyfile.json
© www.soinside.com 2019 - 2024. All rights reserved.