如何使用oauth2client(Google-auth-library)获得Google Workspace附加应用程序的刷新

发布于 2025-02-13 03:38:17 字数 923 浏览 1 评论 0原文

我有一个使用Node.js构建的Google Workspace附加应用程序,并具有以下范围:

"https://www.googleapis.com/auth/gmail.addons.execute",
"https://www.googleapis.com/auth/calendar.addons.execute",   
"https://www.googleapis.com/auth/gmail.readonly"

当提示用户接受范围时,我将获得包含的令牌列表;

userOAuthToken, systemIdToken, userIdToken

这里的问题是,授予的用户是有时的,因此我需要一个刷新令牌才能访问以备后用。我正在尝试访问刷新令牌,但会遇到错误:

错误:未设置刷新令牌。在oauth2client.refreshtokennocache错误。

我正在使用以下代码访问刷新令牌,

    const accessToken = req.body.authorizationEventObject.userOAuthToken;
       const auth = new OAuth2Client();
       auth.setCredentials({access_token: accessToken});
        auth.refreshAccessToken().then((token)=>{
        console.log("token is " + token);
      }).catch((err)=>{
        console.log(err);
      });

该如何解决此错误?

I have a Google Workspace add-on application built with Node.js with the following scopes:

"https://www.googleapis.com/auth/gmail.addons.execute",
"https://www.googleapis.com/auth/calendar.addons.execute",   
"https://www.googleapis.com/auth/gmail.readonly"

When the user gets prompted to accept scopes, I will get a list of tokens included;

userOAuthToken, systemIdToken, userIdToken

The issue here is that the userOAuthToken granted is time-bound, so I need a refresh token to access for later use. I am trying to access refresh token but getting an error:

Error: No refresh token is set. at OAuth2Client.refreshTokenNoCache error.

I am using the following code to access the refresh token

    const accessToken = req.body.authorizationEventObject.userOAuthToken;
       const auth = new OAuth2Client();
       auth.setCredentials({access_token: accessToken});
        auth.refreshAccessToken().then((token)=>{
        console.log("token is " + token);
      }).catch((err)=>{
        console.log(err);
      });

How can I fix this error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文