We can use googleapis npm to implement google oauth2 functionality.
Main thing is to create new one by refresh token when access token is expired.
- Get authorization code from client.
- Get refresh token from authorization code.
- Add following code to generate access token automatically when it is expired
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);});