Answer the question
In order to leave comments, you need to log in
Infinite token for Google Api Service?
Guys, I'm using google/apiclient in a project. To work with this api, you need to log in and get a token with which the library will work. It looks like this:
protected function getClient()
{
$client = new Google_Client();
$client->setApplicationName(APPLICATION_NAME);
$client->setScopes(SCOPES);
$client->setAuthConfig(CLIENT_SECRET_PATH);
$client->setAccessType('offline');
// Load previously authorized credentials from a file.
$credentialsPath = CREDENTIALS_PATH;
if (file_exists($credentialsPath)) {
$accessToken = json_decode(file_get_contents($credentialsPath), true);
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:\n%s\n", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
// Store the credentials to disk.
if (!file_exists(dirname($credentialsPath))) {
mkdir(dirname($credentialsPath), 0700, true);
}
file_put_contents($credentialsPath, json_encode($accessToken));
printf("Credentials saved to %s\n", $credentialsPath);
}
$client->setAccessToken($accessToken);
// Refresh the token if it's expired.
if ($client->getRefreshToken()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
}
return $client;
}
// Refresh the token if it's expired.
if ($client->getRefreshToken()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
}
{"access_token":"ya29.Ci9IA7H-*******-*********","token_type":"Bearer","expires_in":3600,"refresh_token":"1\/**********-*******","created":1471990656}
{"access_token":"ya29.Ci9IA7H-*******-*********","token_type":"Bearer","expires_in":3600,"created":1471990987}
Answer the question
In order to leave comments, you need to log in
Yes, I did. For Google, this is normal. It sends refresh_token only once. If the refresh_token has already been issued to you, then do not wait for it anymore. Just don't overwrite the previous one and use it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question