68

I am having the same error as this. In my case it is happening when the app is trying to upload a file via Google Cloud Client API.

POST https://www.googleapis.com/oauth2/v4/token
400 Bad Request
Invalid JWT: Token must be a short-lived token and in a reasonable timeframe

How I load the token is reading a service-account json file, and attach it to CURLOPT_HTTPHEADER in PHP. It did work properly for the past one month, so I am guessing that Google changed the way of authorization.

Has anyone faced & solved this?

Machavity
  • 30,841
  • 27
  • 92
  • 100
wataru
  • 1,049
  • 1
  • 11
  • 19

10 Answers10

129

I stumbled upon the same issue at roughly the same time, so I expected a generic bug of Google but here is what had happened on my computer :

raise HttpAccessTokenRefreshError(error_msg, status=resp.status)
oauth2client.client.HttpAccessTokenRefreshError: invalid_grant: Invalid JWT: Token must be a short-lived token and in a reasonable timeframe

was caused in my case by a poor synchronisation of the computer's clock where the code was executed that had a lag of 5 minutes (due to a faulty battery for the internal clock). It started working again when I manually changed the internal time of my computer to the correct one.

This solution was mentionned here, but not with the full error message

Community
  • 1
  • 1
WNG
  • 3,705
  • 2
  • 22
  • 31
  • 1
    It is possibly the answer, because I have seen my server's clock is not in sync sometimes. I appreciate that – wataru Mar 24 '16 at 15:57
  • 4
    This appears to be the solution. Specifically, the Docker container I'm using has a busted clock even though my system clock is fine. – plowman Sep 26 '16 at 03:34
  • had the exact same problem here. My computer's clock was 5 minutes out of sync. Thanks for the answer – Caique Moreira Mar 16 '17 at 18:53
  • I was receiving this when I run unit tests and forgot that I also patch the time (yes, I have mock, but I was testing it without mock to check credentials) – dieend Aug 16 '18 at 23:25
  • 1
    That solved it for me. My computer's clock was off by a few hours. Readjusted it and the error went away. – I Stand With Israel Oct 11 '20 at 15:15
22

I had the same issue, I found that my server was delayed by 8 minutes. I configurated the NTP server and magically it was solved

Eddy López
  • 341
  • 1
  • 3
7

For those getting that error for a containerized app and ending up here,

I had the same issue when using my token from a containerized app. Rebooting Docker Desktop (Windows) did it for me.

I had this setup for weeks before getting that issue for the first time.

Dave
  • 2,774
  • 4
  • 36
  • 52
6

Same problem encountered....manually reset my computer's clock to the current time....problem resolved.

Gurdeep Singh
  • 69
  • 1
  • 1
3

For those using vagrant, ssh into your box and run sudo service ntp stop && sudo ntpd -gq && sudo service ntp

Erin
  • 5,315
  • 2
  • 20
  • 36
2

I was stumped on this for hours. I even posted this question. Turns out I had overlooked something that should have been somewhat obvious. My dev server is a Linux virtual machine. The clock on my VM was 8 days behind my local system clock. So while my local clock was correct, I overlooked the fact that my VM was several days delayed. Hopefully, this can save the absent-minded some time.

ajon
  • 7,868
  • 11
  • 48
  • 86
2

I had this problem when using Windows Subsystem for Linux (WSL). All my WSL images were running with date set in the past, https://github.com/microsoft/WSL/issues/4245 suggests that its a problem when Windows resumes from sleep.

I fixed by restarting WSL, wsl --shutdown

jamiet
  • 10,501
  • 14
  • 80
  • 159
1

I had the similar problem and my job was keep failing with below error:

  File "/usr/local/lib/python2.7/site-packages/google/oauth2/_client.py", line 59, in _handle_error_response
    error_details, response_body)
google.auth.exceptions.RefreshError: ('invalid_grant: Invalid JWT: Token must be a short-lived token (60 minutes) and in a reasonable timeframe. Check your iat and exp values and use a clock with skew to account for clock differences between systems.', u'{\n  "error" : "invalid_grant",\n  "error_description" : "Invalid JWT: Token must be a short-lived token (60 minutes) and in a reasonable timeframe. Check your iat and exp values and use a clock with skew to account for clock differences between systems."\n}')
2018-05-1713:01:01:  API CALL iOS intra day: module failed.

The actual root cause was the system time was out of sync with the server. I have refreshed the ntp server which synced the time and the problem was resolved.

double-beep
  • 5,031
  • 17
  • 33
  • 41
jagath
  • 238
  • 1
  • 6
1

I had a similar problem when trying to reach the google cloud big query API. Adjusting my system clock and allowing windows 10 to automatically set the time and time zone solved the problem.

Don Ondeje
  • 19
  • 3
0

It sounds obvious, but ensure the expiry exp is after the issued at time iat.

Mark Tickner
  • 1,023
  • 2
  • 15
  • 26