'Login with Firebase Gmail and Google OAuth invalid_grant / Bad Request when refresh token
Description
I'm having a problem with Google API Client(PHP, Laravel) when I try to refresh the expired token but I get an error.
Array
(
[error] => invalid_grant
[error_description] => Bad Request
)
below is my code to refresh the access token
<?php
require_once __DIR__.'/vendor/autoload.php';
session_start();
function debug($msg) {
echo "<pre>";
print_r($msg);
echo "</pre>";
}
const CLIENT_ID = "563762623872-8ka1114m12g9t8ajjcqp1pf3rhl2mnit.apps.googleusercontent.com";
$idToken = "eyJhbGciOiJSUzI1NiIsImtpZCI6IjQ4NmYxNjQ4MjAwNWEyY2RhZjI2ZDkyMTQwMThkMDI5Y2E0NmZiNTYiLCJ0eXAiOiJKV1QifQ.xxxx";
$refreshToken = "AIwUaOmGtgE12PnksmP-KUC18uHjr29TDHBPhlsFK0gcVx_YkvK4pE8noVLop0N57NWZYaWaPfoqa1KIBPj6b0q7uitqv2MSjW9Y4rZScw6vof7mAY1IGDkrduD_rB8rMFwyXvPeTo=--xxxx";
$accessToken = "ya29.a0ARrdaM-Hvs88Dya4wZ5ZX1La7qQRr5YCi4YX1JmEjF1d07T35Mm_9r2jua1z6H0Py4IKXXcFhmzTo0textFje0_QAWvox0ebvB0YvYlEQp5oW4nYTlTH_Q-xxxx";
function createToken($accessToken, $refreshToken, $idToken = null)
{
$token['access_token'] = $accessToken;
$token['refresh_token'] = $refreshToken;
$token['token_type'] = "Bearer";
$token['expires_in'] = 3600;
$token['id_token'] = $idToken;
return $token;
}
$token = createToken($accessToken, $refreshToken, $idToken);
$client = new Google_Client(['client_id' => CLIENT_ID]);
$client->setAuthConfig('client_secrets.json');
$client->setAccessType('offline');
$client->setPrompt('select_account consent');
$client->setAccessToken($token);
debug($client->refreshToken($client->getRefreshToken()));
STEP
I log in with Gmail from front-end (my website) and it returns access token and refresh token
Send access token, refresh token and id token to server.
Everything is ok. But after 1 hour the token expires.
Refresh Token and appear error like above
I have tried many ways but not working. Hope everyone can help, thank you all.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
