刷新Google API令牌以保持用户login状态

我正在开发一个使用Google日历的Google操作应用程序。 在用户第一次互动开始时,系统会提示他们链接其Google帐户以访问日历。 但是对于几乎每个将来的交互,用户都必须再次进行authentication。

我的代码从用户数据获取访问令牌如下。 我不确定是否有办法将刷新令牌添加到凭据中。 我已经看到你应该在其他答案的请求中包含"access_type": "offline" ,但是我不确定将它放在我的代码中。 任何帮助将不胜感激!

 const googleAuth = require('google-auth-library'); function getAuth(app) { var accessToken = app.getUser().accessToken; var clientId = CLIENT_ID; var clientSecret = CLIENT_SECRET; var redirectUrl = 'https://oauth-redirect.googleusercontent.com/r/my_app_name'; var auth = new googleAuth(); var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl); var token = { "access_token": accessToken, "refresh_token": "", "access_type": "offline", //Not sure if these attributes should be here "approval_prompt": "force", "immediate": true, "token_type": "Bearer" }; oauth2Client.credentials = token; return oauth2Client; } calendar.freebusy.query ({ auth: getAuth(app), //need a Google OAuth client here for API request headers: { "content-type" : "application/json" }, resource:{ items: [ { id: "primary" } ], timeMin: startDate.format(), timeMax: endDate.format(), timeZone: timeZoneId }, function(err, response) { console.log('Success!'); } }); 

不知道这是否有帮助,但在第一次互动时,用户必须select一个帐户并同意日历权限,但是在后续交互中,他们只需点按“将帐户链接到Google”,并自动对其进行身份validation。

编辑:

这是与应用程序每次新的交互的提示的屏幕截图 。 我的理解是,只有在与应用程序进行第一次交互时才进行帐户链接,然后帐户链接良好,用户不需要再次提示。 我没有收到任何错误,我只是想知道是否以及如何修改我的当前代码,以不提示用户在每次交互中链接其帐户。