Reputation: 113
I'm using the Google APIs Client Library for PHP for offline authentication, but when I use this code:
$this->google->refreshToken($result["google_refresh_token"]));
it returns NULL.
$this->google
refers to an instance of the Google_Client class.
Anyone an idea what this could be?
If I try to change the given refresh token it returns a Google Exception Error, so it should be a valid token.
Thank you!
Edit:
'client_id' => 'myclientid',
'client_secret' => 'myclientsecret',
'redirect_uri' => 'mypage',
'developer_key' => 'mydeveloperkey',
// Other parameters.
'access_type' => 'offline',
'approval_prompt' => 'force',
Maybe this helps you
Upvotes: 6
Views: 5759
Reputation: 63
I have a similar problem, but for the latest version of the PHP google apiclient library, when I changed it to 2.0 everything works as it should.
"google/apiclient": "^2.0",
Now all I have to do is check if the token is active, if not, then ask for a new token, and in response I get a new token and a new refreshtoken, which I save to download another one and so on. This is my sample code:
$google_client = new \Google_Client();
$google_client->setClientId('');
$google_client->setClientSecret('');
$google_client->setRedirectUri('');
$google_client->addScope('email');
$google_client->addScope('profile');
$google_client->addScope(Gmail::GMAIL_READONLY);
$google_client->setAccessType ("offline");
$google_client->setApprovalPrompt ("force");
if( !$google_client->isAccessTokenExpired() ) {
$refreshToken = $google_client->fetchAccessTokenWithRefreshToken( Session::get( 'refresh_token' ) );
}
Upvotes: 0
Reputation: 11104
I was hitting my head against a wall for a very long time.
This is not immediately obvious but:
define('CREDENTIALS_PATH', '/vendor/google/credentials/credentials.json');
The line that references "credentials.json" or whatever your credentials file will be called-- this file should not exist when you start! If you have created that file before getting authorization– delete it then visit your script page– quickstart.php
or whatever, and it will then ask you for authorization.
If you've tried refreshing the page over and over again there are usage limits (i.e. requesting authorization too many times), so you might need to create a new client_id in your developer console and update your define('CLIENT_SECRET_PATH')
path.
I then added this:
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$jsonCred = file_get_contents(CREDENTIALS_PATH);
$jsonArray = json_decode($jsonCred, true);
$client->fetchAccessTokenWithRefreshToken($jsonArray["refresh_token"]);
$newAccessToken = $client->getAccessToken();
$accessToken = array_merge($jsonArray, $newAccessToken);
file_put_contents($credentialsPath, json_encode($accessToken));
}
```
Upvotes: 0
Reputation: 1279
I have had a similar issue. Check my detailed answer here
Here is the quick solution
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
$newAccessToken = $client->getAccessToken();
$accessToken = array_merge($accessToken, $newAccessToken);
file_put_contents($credentialsPath, json_encode($accessToken));
}
Upvotes: 3
Reputation: 113
I now use a really simple method to avoid the refresh_token()
method.
I just save the whole JSON response from the first request and give this to the setAccesToken()
method. If the access token expires, it will automatically get a new one from the given JSON.
Upvotes: 5
Reputation: 365
I was having this same issue for quite a while. For me it was because, for some reason, Google only sends you the refresh token the first time you request the token. So you have to make sure you store that refresh token for that user for all of entirety because Google wont give it to you again.
For development this can be a real pain. You can get Google to send you a new refresh token but what you have to do is deauthorize whatever account you authorized through your application. You can do this in your Google security settings.
Hope this helps!
Upvotes: 14