37

I am using Google's php api client. I am running through the quickstart guide for service accounts. I followed the steps perfectly (as far as I can tell). I am running into the following error:

{
   "error": "invalid_grant",
   "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."
}

From what I have read the most common problem with this error is if the system time is wrong. I have triple checked that my timezone and date and time are synced with the atomic clock. I used php set timezone function to set my timezone to match my computer, but I continue to get the error. I am looking into the other part of the message that mentions the iat and exp settings, and haven't gotten anywhere yet.

Does anyone have any ideas of how I can get past this?

1

11 Answers 11

64

Invalid_grant error has two common causes:

  1. Your server’s clock is not in sync with NTP.

    Solution: Check the server time. If it's incorrect, fix it.

  2. The refresh token limit has been exceeded.

    Solution: Nothing you can do - they can't have more refresh tokens in use.

    Applications can request multiple refresh tokens. For example, this is useful in situations where a user wants to install an application on multiple machines. In this case, two refresh tokens are required, one for each installation. When the number of refresh tokens exceeds the limit, older tokens become invalid. If the application attempts to use an invalidated refresh token, an invalid_grant error response is returned.

    The limit for each unique pair of OAuth 2.0 client and is 50 refresh tokens (note that this limit is subject to change). If the application continues to request refresh tokens for the same Client/Account pair, once the 26th token is issued, the 1st refresh token that was previously issued will become invalid. The 27th requested refresh token would invalidate the 2nd previously issued token and so on.

Update

Applications in testing phase have their refresh tokens revoked after seven days. Go to Google cloud console for your project and under oauth2 set it to production.

3
  • i think the refresh token limit is now 50 Feb 7, 2020 at 11:58
  • 6
    Unbelievable, I was despaired and when I tried to simply change my computer time (it has 5(!) minutes in advance), the test that failed to run, started to work! Awesome!
    – Aaron Tuil
    Sep 2, 2021 at 9:46
  • its called clock skew if your interested. Most of the google client libraries are designed to request a new access token five minutes before the old one is due to expire. But with your clock being off its going to have issues validating it. Sep 2, 2021 at 9:50
23

Fixed the issue for me in 2020.

  • If on Windows, right click the date and time on the lower right.
  • Click "Adjust Date/Time".
  • Click "Sync now."
0
12

I had this with WSL2 on Windows - sometimes when the laptop hibernates, the WSL time falls behind the system time.

Run sudo hwclock -s to sync.

0
10

WOW!!! This ended up being something really stupid. I am running Laravel's homestead as my dev server. I mostly assumed that the clocks on my VM and local machine would be synced. At least at the time of creation. But, I went a couple weeks without using this machine, so (I'm guessing), the VMs clock wasn't running while this computer was in hibernation, or the clocks were never in sync. Regardless, the issue is that my vm's clock was about 9 days behind my system clock. That was causing the issue.

3
  • Wow, I'm using Laravel Homestead, too, and it never occurred to me that it could have a different system time. Thanks.
    – Ryan
    Mar 14, 2018 at 16:12
  • I was running Vagrant test VMs. My VM was at least a day out of sync due to suspending my laptop.
    – ayke
    Jun 15, 2019 at 21:31
  • My case was also too stupid... I left my VM running a program(Wind + L) when I came back this error was printed. NTP clocks were disincronized, just restard your VM
    – Roger Coll
    Jul 26, 2019 at 13:11
1

if you run localy, check the time zone of your computer, for me it was the problem, the best solution in this case is to set it to automatic

1

Check your system time once. It may happen that you have dual boot because of which Windows couldn't update the latest timezone. Just refresh it once again in settings and see the magic.

Don't know why that dependency but that's how it is!

1

In Linux, I solved this problem by selecting the automatic date and time option.

1
  • I think your answer would be more helpful if you also share "how" and "where" you did that.
    – Rub
    Dec 23, 2022 at 15:12
0

For my case, I just need to restart my machine. (Faced the issue on vagrant VM). It's working again.

0

I found a solution by changing the time zone to that of the server:

date
Output:
Wed Apr 26 17:44:38 UTC 2017 

and then:

timedatectl list-timezones
sudo timedatectl set-timezone America/New_York
Output:
Wed Apr 26 13:55:45 EDT 2017
0

First I doubted about changing server time. Because it's only 5 minutes differences between server and my computer(network time). I've been looking for various methods related to oAuth, but I have changed the server time in case, and it works well amazingly.

3
-1

The first thing to check is the date and time. But remember that the time in the execution environment of your code may differ from the system time. For example, if you are working with tests and time is mocked (in my case, I copied another test case and forgot to remove the line with mockdate npm library: MockDate.set("4/15/2022 21:30:00")

So the first thing to do is to ensure that printing the current date-time shows the actual time, e.g. echo date("m/d/Y h:i:s a", time()).

Your Answer

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.