Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

Google rejects the service-account JWT that my PHP server signs

Your error

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.1 more line it can show

Fix it fast

Most common cause, 1 of 2

The server clock is wrong

Switch network time sync on and let the clock settle.

Fix
sudo timedatectl set-ntp true

# /etc/systemd/timesyncd.conf
[Time]
NTP=
FallbackNTP=time.google.com

sudo systemctl restart systemd-timesyncd
timedatectl timesync-status
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

invalid_grant: Invalid JWT: Token must be a short-lived token (60 minutes) and in a reasonable timeframe means Google checked the iat and exp in your service-account assertion against its own clock. The usual reason is a wrong server clock, so turn on NTP sync. If you build the JWT yourself, keep exp within one hour of iat.

  • Applies to google/apiclient 2.20, google/auth 1.55, PHP 8.2 to 8.6, Linux servers with systemd
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Compare the server's UTC time with the Date header of a Google response. A gap of more than a minute or two points to the clock; matching times point to the claims in the JWT.

Run this first
date -u
curl -sI https://oauth2.googleapis.com/token | grep -i '^date:'
timedatectl

Then find the line you see in this list and open its fix:

  1. 1The server clock is wrongdate -u and the Date header differ by minutes, and timedatectl shows network time sync as inactive or not synchronized
  2. 2A hand-built assertion puts exp more than an hour after iatThe clock is correct, your own code (or a changed expiry setting) builds the JWT, and decoding it shows exp minus iat above 3900 or exp below iat

What does the error look like?

  • 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.
  • invalid_grant

Cause 1 of 2

The server clock is wrong

You seedate -u and the Date header differ by minutes, and timedatectl shows network time sync as inactive or not synchronized

Check

Google's JWT error table says this message usually means the local system time is not correct. The PHP library takes iat and exp from the server clock (google/auth sets iat 60 seconds in the past and exp one hour ahead), so a clock that runs minutes fast or slow puts the pair outside the window Google accepts.

Check
timedatectl status

Fix

Switch network time sync on and let the clock settle. If the machine has no working time source, Google suggests its public NTP service; with systemd-timesyncd it goes in /etc/systemd/timesyncd.conf.

Fix
sudo timedatectl set-ntp true

# /etc/systemd/timesyncd.conf
[Time]
NTP=
FallbackNTP=time.google.com

sudo systemctl restart systemd-timesyncd
timedatectl timesync-status

Verify

The two times match, and a fresh token request through the client succeeds.

Verify
date -u; curl -sI https://oauth2.googleapis.com/token | grep -i '^date:'
php -r 'require "vendor/autoload.php"; $c = new Google\Client(); $c->setAuthConfig("service-account.json"); $c->addScope("https://www.googleapis.com/auth/drive.readonly"); var_dump(isset($c->fetchAccessTokenWithAssertion()["access_token"]));'

Confirmed by developers.google.com

Did this fix it?

Cause 2 of 2

A hand-built assertion puts exp more than an hour after iat

You seeThe clock is correct, your own code (or a changed expiry setting) builds the JWT, and decoding it shows exp minus iat above 3900 or exp below iat

Check

Google caps exp at one hour after iat and returns this error when exp is more than 65 minutes after iat, or lower than iat. Typical slips in custom code are time() + 7200, or a millisecond value in one claim and seconds in the other.

Check
php -r '$p = explode(".", $argv[1])[1]; $c = json_decode(base64_decode(strtr($p, "-_", "+/")), true); echo $c["exp"] - $c["iat"], PHP_EOL;' "$JWT"

Fix

Take both claims from one time() call in seconds and keep the span at one hour, or let google/apiclient sign the assertion from the key file with setAuthConfig.

Fix
$now = time();
$claims = [
    'iss'   => $serviceAccountEmail,
    'scope' => 'https://www.googleapis.com/auth/drive.readonly',
    'aud'   => 'https://oauth2.googleapis.com/token',
    'iat'   => $now,
    'exp'   => $now + 3600,
];

Verify

The decoded span prints 3600 or less, and the token endpoint answers with an access token.

Verify
php -r '$p = explode(".", $argv[1])[1]; $c = json_decode(base64_decode(strtr($p, "-_", "+/")), true); echo $c["exp"] - $c["iat"], PHP_EOL;' "$JWT"

Confirmed by developers.google.com

Did this fix it?

How do I stop it coming back?

  • Make NTP sync part of server provisioning and alert when timedatectl reports the clock as unsynchronized.
  • Let google/apiclient or google/auth build service-account assertions instead of hand-rolled JWT code.
  • Log the server's UTC time next to any invalid_grant response, so a clock problem is visible in the first log line.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: Spotless Laundry, EZ Estimater.

Free tool for this

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.