1
votes

I'm trying to get a new access_token after he expired. I already keep in the bank the information I receive in return the first client access:

{"access_token": "TOKEN", "refresh_token": "TOKEN", "token_type", "Bearer", "expires_in": 3600, "created ": 1320790426}

After that token expires I need to apply for a new one, I am doing this way:

$client = new Google_Client();
$client->setApplicationName(APPLICATION_NAME);
$client->setScopes(SCOPES);
$client->setAuthConfigFile(CLIENT_SECRET_PATH);
$client->setAccessType('offline');

$token['access_token']  = $db->dados[0]['use_access_token'];
$token['refresh_token'] = $db->dados[0]['use_refresh_token'];
$token['token_type']    = $db->dados[0]['use_token_type'];
$token['expires_in']    = $db->dados[0]['use_expires_in'];
$token['created']       = $db->dados[0]['use_created'];

$client->setAccessToken(json_encode($token));

if ($client->isAccessTokenExpired()) {
    echo "Expired !";
    $client->refreshToken($client->getRefreshToken());
    //$accessToken = $client->authenticate($authCode);
} else {
    ...
}

The problem is that always returns this error: Error refreshing the OAuth2 token, message:

{ "error" : "invalid_grant" }

1
how did you obtain, extract and store the refresh token in the database (i.e. the value you retrieve by $db->dados[0]['use_refresh_token']); you may want to paste that code as wellHans Z.

1 Answers

0
votes
    $client->refreshToken($refreshToken);
    $newtoken=$client->getAccessToken();
    echo $newtoken."</br>";