Google-api-nodejs-client: Clarification: How to handle instantiation of Oauth2Client when dealing with multiple tokens

Created on 19 Sep 2017  路  1Comment  路  Source: googleapis/google-api-nodejs-client

In my app I have multiple users who I have tokens for, and a function to retrieve their google plus profile. In this function should I be re-instantiating an Oauth2Client so that when I call setCredentials() it doesn't change the credentials of a global client? Something like this

function getProfile(token) {
  const oauth2Client = new OAuth2(
    'id',
    'secret,
    'https://redirect.url'
  );
  oauth2Client.setCredentials(token);
  gmail.users.getProfile({userId: '', auth:oauth2Client})...

OR is it smart to use a global oauth client, and just call setCredentials() every time inside the function. Hopefully credentials aren't mixed up between a-sync calls?

const oauth2Client = new OAuth2(
  'id',
  'secret,
  'https://redirect.url'
);

function getProfile(token) {
  oauth2Client.setCredentials(token);
  gmail.users.getProfile({userId: '', auth:oauth2Client})...

Most helpful comment

Greetings! I would advise creating a new OAuth2Client for each set of users. There's a fair bit of caching and such that happens inside of the client objects, and I wouldn't trust data not to get mixed up. Hope this helps!

>All comments

Greetings! I would advise creating a new OAuth2Client for each set of users. There's a fair bit of caching and such that happens inside of the client objects, and I wouldn't trust data not to get mixed up. Hope this helps!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ACMerriman picture ACMerriman  路  3Comments

Chethandsagar picture Chethandsagar  路  4Comments

lowagner picture lowagner  路  3Comments

ovaris picture ovaris  路  3Comments

suparnavg picture suparnavg  路  3Comments