Using the following event, my backend is notified when an access token is refreshed.
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);
});
But is there a way to know the predecessor of new access token? I store multiple couples of the access token and refresh token in DB(multiple users/accounts), where all of them are used to get data in parallel.
How can I update expired access token, stored in my DB, with the newer one? I could, of course, keep using the old access token and never update it, as the library is able to get a new one on every request, but this will make too many unnecessary requests.
I've figured it out. The oauth2Client
object contains credentials
property which has the old access_token
. So I just check it like this:
if (oauth2Client.credentials.access_token) {
// update it in DB
}