Google API - 使用库侦听过期令牌
Google API - listen for expired token with library
我已经将 oauth2 流程设置为使用 google API 和 refresh_token。
我正在使用 nodejs 库发出请求。我将完整的令牌(刷新+访问)存储在数据库中并将其传递给图书馆。现在每次访问令牌过期时,库都会使用提供的 refresh_token.
自动更新它
问题是:由于库会在需要时自动更新令牌,我如何才能获得更新后的刷新令牌以便在后续调用中传递它?
我知道我可以向自己发出 "refreshToken" 调用,但只要库在需要时自动调用,我就可以立即访问更新后的令牌吗?
我想我在文档中看到了类似的东西(在令牌刷新时设置一种监听器?)但我找不到了。
终于再次找到 part of the doc,其中显示了如何侦听令牌过期:
Handling refresh tokens
Access tokens expire. This library will
automatically use a refresh token to obtain a new access token if it
is about to expire. An easy way to make sure you always store the most
recent tokens is to use the tokens event:
oauth2Client.on('tokens', (tokens) => {
if (tokens.refresh_token) {
// store the refresh_token in my database!
console.log(tokens.refresh_token);
}
console.log(tokens.access_token);
});
我已经将 oauth2 流程设置为使用 google API 和 refresh_token。
我正在使用 nodejs 库发出请求。我将完整的令牌(刷新+访问)存储在数据库中并将其传递给图书馆。现在每次访问令牌过期时,库都会使用提供的 refresh_token.
自动更新它问题是:由于库会在需要时自动更新令牌,我如何才能获得更新后的刷新令牌以便在后续调用中传递它?
我知道我可以向自己发出 "refreshToken" 调用,但只要库在需要时自动调用,我就可以立即访问更新后的令牌吗?
我想我在文档中看到了类似的东西(在令牌刷新时设置一种监听器?)但我找不到了。
终于再次找到 part of the doc,其中显示了如何侦听令牌过期:
Handling refresh tokens
Access tokens expire. This library will automatically use a refresh token to obtain a new access token if it is about to expire. An easy way to make sure you always store the most recent tokens is to use the tokens event:
oauth2Client.on('tokens', (tokens) => {
if (tokens.refresh_token) {
// store the refresh_token in my database!
console.log(tokens.refresh_token);
}
console.log(tokens.access_token);
});