69

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?

4
  • 1
    I didn't do anything but it's solved. Anyone have thoughts?
    – wataru
    Mar 23, 2016 at 22:49
  • 1
    Here is my answer enter link description here Mar 31, 2016 at 11:53
  • 4
    @MichaelMishKisilenko apparently the unsynchronized internal clock causes. Thank you for sharing yours!
    – wataru
    Mar 31, 2016 at 15:27
  • This is mainly time related issue on your client mostly.I have this issue on my android emulator.Its time was incorrectly set as per timezone. Apr 23, 2017 at 9:08

10 Answers 10

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

5
  • 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, 2016 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, 2016 at 3:34
  • had the exact same problem here. My computer's clock was 5 minutes out of sync. Thanks for the answer Mar 16, 2017 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, 2018 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. Oct 11, 2020 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

3
  • 10
    just running ntpdate ntp.ubuntu.com worked for me, thanks! Sep 1, 2017 at 19:42
  • So this keeps on happening how can I make sure that the server time is matched up all the time? I keep on getting the same error... Jan 12, 2018 at 17:44
  • after I tried everything, I found your answer, and it worked for me! Sep 7, 2020 at 13:06
7

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

2
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.

2
  • 2
    ya. me too. probably because Im hibernating my machine. So its eventually confusing docker
    – Dilushan
    Jan 31, 2020 at 15:15
  • 1
    same here with containerized .net core app. Feb 19, 2020 at 13:31
3

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

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.

2

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.

1
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

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.

0

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.