In PHP, I use OAuth2 to connect and save data to Google spreadsheet. After authenticating, I get refreshToken and accessToken.
In the first hour, accessToken is used and everything works fine.
When accessToken expired, I used refreshToken to get new accessToken. But I always receive the error "Bad request" instead of new accessToken.
How to fix this issue?
$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->setScopes([Google_Service_Sheets::SPREADSHEETS]);
$client->setAccessType('offline');
$client->setAccessToken($acc_token);
First way:
if ($client->isAccessTokenExpired()) {
$client->refreshToken($refresh_token);
$newAccessToken = $client->getAccessToken();
$client->setAccessToken($newAccessToken);
}
Second way:
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($refresh_token);
$newAccessToken = $client->getAccessToken();
$client->setAccessToken($newAccessToken);
}
I've tried some answers of the following questions but it's not solved my issue:
Getting new accesstoken using the refreshtoken already retrieved in googledrive api