Reputation: 934
I have the following code running on my localhost to try and authenticate with Google Oauth2. After authenticating, it appears to work for a while. Then after a certain amount of time I'm getting this classic error:
Error refreshing the OAuth2 token, message: '{
"error" : "invalid_grant"
}'
I've looked at the previous times this issue was posted to StackOverflow, but the suggested solutions didn't seem to work for me.
I believe the access token is updated correctly (see code below). I also believe the server clock is synchronized correctly. What am I doing wrong?
//$token retrieved from database
$token = {"access_token":"xxx.xxx_xxxx-xxx","token_type":"Bearer","expires_in":3600,"refresh_token":"1\/xXxXxxxxxXXXxx","created":145490000}
$google = new Google_Client();
$google->setAccessType('offline');
$google->setClientId($client_id);
$google->setClientSecret($client_secret);
$google->setRedirectUri($client_redirect_uri);
$google->setAccessToken($token);
if($google->isAccessTokenExpired()) {
$google->refreshToken($token);
}
$token = $google->getAccessToken();
$google->setAccessToken($token);
//Some Database code to save the new $token
...
$result = new \Google_Service_YouTube($google);
Upvotes: 3
Views: 7342
Reputation: 116948
There are two posibple causes for Invalid_Grant
You stated that your code works for a while then stops working. This makes me think your problem is nr 2.
Reasons why a refresh token will stop working:
To solve your problem you need to try and figure out which of the 3 problems above has caused your refresh token to expire. I cant really help you much with that its going to require some detective work on your part.
Upvotes: 5