'googleapis library, multiple requests handle

I'm trying to figure out how to use 'googleapis' library with a lot of requests.

Initialize oauth2Client using 'googleapis' library

const {google} = require('googleapis');

const oauth2Client = new google.auth.OAuth2(
  YOUR_CLIENT_ID,
  YOUR_CLIENT_SECRET,
  YOUR_REDIRECT_URL
);

Everything goes fine, then I take the refresh_token and save it somewhere in the database. Now with each user request, I need to somehow get the oauth2Client instance or create a new one via new(like from above) and pass the saved refresh_token via setCredentials

oauth2Client.setCredentials({
    refresh_token: savedToken
});

in this case, a new access_token will be generated on every request, anyway now I can use library API to create requests by passing oauth2Client

const oauth2 = google.oauth2({
    version: 'v2',
    auth: oauth2Client
})

Well, first of all, is it normal to do this? When using an already created (one instance), I'm afraid something may not work correctly and the request will be made with a different token.

and also with each request, since I pass the refresh_token, a new access_token will be created, which I also wanted to avoid.

It is desirable that I can use this library, and that the token is not refreshed with each request.

or maybe it's better to create a class in which I make requests myself, something like, but in this case i need to control and refresh token manualy

   class DocsGoogle {
      ...
      getAll () {
         // make HTTP request with tokens what needs and return 
      }
      ...
   } 

How is best to do this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source