Google-api-nodejs-client: Does access token Expires

Created on 22 Feb 2016  Â·  4Comments  Â·  Source: googleapis/google-api-nodejs-client

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?

Most helpful comment

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?

All 4 comments

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:

  • Expected behavior
  • Actual behavior
  • Code samples
  • Version numbers
  • References docs or other links

Or, consider opening a Pull Request.

Otherwise, support questions are handled on Stack Overflow. Ask a question on Stackoverflow

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?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JustinBeckwith picture JustinBeckwith  Â·  3Comments

dibeesh picture dibeesh  Â·  4Comments

nanu-c picture nanu-c  Â·  3Comments

suparnavg picture suparnavg  Â·  3Comments

CyberT33N picture CyberT33N  Â·  3Comments