So I'm trying to get refreshing the access token to work. When the access token is expired, I run refreshToken() and pass the refresh_token to get a new access_token from Google.
Sometimes it seems to work, but otherwhiles I get the error message invalid_grant after that. Seems like I can't pass more than a few days without re-authorising access to my YouTube channel.
Anything essential I am doing wrong?
if ($client->getAccessToken()) {
if($client->isAccessTokenExpired()) {
$newToken = $client->getAccessToken();
//Run refreshToken() and pass in the refresh token to get a fresh access token.
$client->refreshToken($newToken['refresh_token']);
//Take old key object and replace everything except for refresh_token
$newKey = $client->getAccessToken();
$newKeyWithRefreshToken = json_decode($oldKey);
$newKeyWithRefreshToken->access_token = $newKey['access_token'];
$newKeyWithRefreshToken->token_type = $newKey['token_type'];
$newKeyWithRefreshToken->expires_in = $newKey['expires_in'];
$newKeyWithRefreshToken->created = $newKey['created'];
//save to db
DB::getInstance()->update('channel', $channelId , array(
'credentials' => json_encode($newKeyWithRefreshToken)
));