googleapis / google-auth-library-nodejs

🔑 Google Auth Library for Node.js
Apache License 2.0
1.71k stars 375 forks source link

How to properly update Access token #1882

Open dzhiganov opened 1 week ago

dzhiganov commented 1 week ago

Please make sure you have searched for information in the following guides.

Documentation Request

Hey there!

I found some information in the docs that I find confusing and would like to clarify.

In the OAuth2 section, when we call getAuthenticatedClient(), it seems like this should be done once, meaning we have a single client. However, in that case, I don’t understand how this part works: oAuth2Client.credentials.access_token. If we have multiple users, which user’s credentials would oAuth2Client.credentials return?

Or am I misunderstanding, and we actually need to create a new OAuth client instance for each user?

Also, I’m confused about the concept of setCredentials. For which user are the credentials being set? It seems like we should have multiple OAuth client instances.


My next question is about client.on('tokens'). I don’t understand for which user this handler will trigger. It seems like it would make more sense if we have multiple clients.

I’d also really appreciate it if you could explain the proper workflow for using client.on. Do we need to call setCredentials, and then, when the user makes a request with an expired access token, retrieve the token from oAuth2Client.credentials and update it on the client side? Is that correct? Also, I assume we should only update tokens if the provided access token is expired but still valid, right?

Sorry for asking so many questions at once, and thanks for your help!

danielbankhead commented 1 week ago

Hey @dzhiganov, the OAuthClient is designed for 1 context (e.g. user) at a time.