Felsher70 Felsher70 -4 years ago 203
Java Question

How does GoogleCredential token refresh work?

Here are my Credential and Analytics object customized via builders:

GoogleCredential credential = new GoogleCredential
.setClientSecrets("ClientSecretId", "Secret")
.addRefreshListener(new CustomClientCredentialRefreshListener("SomeInfo", "AnotherInfo"))


.setExpirationTimeMilliseconds(1472659276L); //future date in epoch time

Analytics analytics = new Analytics.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)

Why is the access token going to refresh when I call any api request? For example, when I get accounts:

Accounts accounts = analytics.management().accounts().list().execute();

I have the response. It's ok and nothing is wrong with it.

But the access token was not expired, yet. The
variable proves it here:

However it is still refreshing and successfully returning a new access token to my refresh listener. Why?

What is

Do I need to check expiration? Then do I set the refresh token in the credential only in the case that
? Then, in other cases, I just set access token without refresh?

Answer Source

Solved! Problem was in Credendial method "getExpiresInSeconds()" that returns:

(expirationTimeMilliseconds - clock.currentTimeMillis()) / 1000;

My expirationTimeMilliseconds is 1472659276L

currentTimeMillis returns for example: 1472734893827 (3 digits bigger than my nubmer)

  public void intercept(HttpRequest request) throws IOException {
try {
  Long expiresIn = getExpiresInSeconds();
  // check if token will expire in a minute
  if (accessToken == null || expiresIn != null && expiresIn <= 60) {
    if (accessToken == null) {
      // nothing we can do without an access token
  method.intercept(request, accessToken);
} finally {

Token goes to refresh every time because

if (accessToken == null || expiresIn != null && expiresIn <= 60)

is true every time

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download