I see that this pull request adds a method to refresh the access token using a saved refresh token. It is not clear to me how to use it. I have saved the tokens (including the refresh token) from the original getToken request and am now retrieving the token from the Database in a new session. How do I set the credentials on OAuth2Client so that I can call refreshAccessToken and get a new accesstoken?
Asked
Active
Viewed 7,859 times
2 Answers
16
I had missed the paragraph on setting credentials on the github readme, so here is some sample code in case anybody else needs it.
var googleOauth2Client = new OAuth2Client(googleClientId,googleClientSecret, googleCallbackUrl);
googleOauth2Client.setCredentials({
refresh_token: saved_refresh_token
});
googleOauth2Client.refreshAccessToken(function(err, tokens){
response.send({
access_token: tokens.access_token
});
});

Linus Unnebäck
- 23,234
- 15
- 74
- 89

kpg
- 7,644
- 6
- 34
- 67
-
you should choose your answer as the answer! – DTrejo Jan 24 '14 at 20:21
-
3Hello. Thanks for your response but, could you expand it a little bit? I can't see where response is defined. Is googleCallBackUrl mandatory? This is very hard for me to understand. If you post a complete example I will be very grateful. – Danielo515 May 01 '14 at 13:56
-
3As a note for others, it seems `refreshAccessToken` is deprecated. Please correct me if I'm wrong here. – alexbea May 08 '18 at 21:58
-
I cloned the repo and searched, and found this in a CHANGELOG: _Note: `refreshAccessToken` is no longer deprecated._ – Benji Jun 17 '23 at 09:25
0
Just a guess as I haven't used this library. But it looks to me like you simply call myOAuth2Client.refreshAccessToken(function(err, newCredentials){})
, where you have already instantiated the OAuth2Client
object with the old token. (dunno how you do that but it might be as simple as instantiating the object then myOauth2Client.credentials.refresh_token = 'foobar'
.) And if there's no error, it'll modify the OAuth2Client.credentials
object and additionally pass the credentials object to the callback.

Plato
- 10,812
- 2
- 41
- 61