I'm using the API (v1) in the following way:
var oauth2Client = new OAuth2(getGoogleCredentials().clientId, getGoogleCredentials().secret);
oauth2Client = new OAuth2(this.clientId, this.clientSecret);
this.oauth2Client.setCredentials({
access_token: this.access_token,
refresh_token: this.refresh_token,
expiry_date: true
});
never got the 401 or 403 response .
currently i see the behavior that access token may be automatically refreshes.
1) Does gmail Api automatically refreshes the access token?
2) How do i hit with 401 & 403 response and how the gmail api refreshes it?
1) Does gmail Api automatically refreshes the access token?
Yes, that should be the case. From README.md
:
OAuth2 client that allows you to retrieve an access token and refreshes the token and retry the request seamlessly if token is expired.
2) How do i hit with 401 & 403 response and how the gmail api refreshes it?
If the OAuth2 client discovers that the access token is expired, then it should automatically refresh the token. You won't see the 401 or 403 for an expired token.
@ryanseys Can confirm.
Closing as this does not appear to be an issue. If you are convinced that this is really a bug, please feel free to re-open the issue and add more information such as:
Or, consider opening a Pull Request.
Otherwise, support questions are handled on Stack Overflow.
If the OAuth2 client discovers that the access token is expired, then it should automatically refresh the token. You won't see the 401 or 403 for an expired token.
Having oauth2Client
,
Does anyone know the way to check if the token was refreshed and get a new one? I have never found this in the docs. This stands for a common reason — I want to keep always up-to-date token in my database.
Thanks!
UPD
The one way I found for now is the next one:
let storedToken = { access_token: "...", expiry_date: 0000, ... };
oauth2Client.getAccessToken((err, access_token) => {
if (storedToken.access_token !== access_token) {
Object.assign(storedToken, oauth2Client.credentials || {});
}
// next steps go here
})
And there is no other way to access oauth2Client.credentials
than directly. I suggest creating and documenting getter for this, what do you think?
I also face the same issue. I was originally going call oauth2Client.refreshAccessToken()
manually and save the updated token, but it seems that this method is deprecated.
Is the method that @ZitRos posted the elegant solution or has someone discovered a better way?
Most helpful comment
Having
oauth2Client
,Does anyone know the way to check if the token was refreshed and get a new one? I have never found this in the docs. This stands for a common reason — I want to keep always up-to-date token in my database.
Thanks!
UPD
The one way I found for now is the next one:
And there is no other way to access
oauth2Client.credentials
than directly. I suggest creating and documenting getter for this, what do you think?