isAccessTokenExpired seems to behave differently from v2.2 to 2.10
I upgraded from "google/apiclient": "2.2.2", to "google/apiclient": "2.10.1". in my code curl call, I have been doing a refresh token.
public function refreshToken()
{
if ($this->googleClient->isAccessTokenExpired() || empty($this->oauth2Token)) {
$result = $this->googleClient->fetchAccessTokenWithAssertion();
if (!isset($result['access_token'])) {
throw new \Exception('unexpected response: ' . json_encode($result));
}
$this->oauth2Token = $result['access_token'];
}
return $this->oauth2Token;
}
Either \Google\Client::isAccessTokenExpired is not returning true Or \Google\Client::fetchAccessTokenWithAssertion is not returning a token.
Once my process hit the hour mark, I get
{"error":{"code":401,"message":"Request had invalid authentication credentials.
Expected OAuth 2 access token, login cookie or other valid authentication credential.
See https://developers.google.com/identity/sign-in/web/devconsole-project.","status":"UNAUTHENTICATED"}}
The code will creates a new token in google/apiclient": "2.2.2"
Is it possible, I am doing a refresh I do not need to do for a service account? Should I just get a new token for each request?
Can you post all of your code. If you are using a service account then no you shouldn't bother with refreshing the library does that for you.
putenv('GOOGLE_APPLICATION_CREDENTIALS='.__DIR__.'/service-account.json');
function getServiceAccountClient() {
try {
// Create and configure a new client object.
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope([YOUR SCOPES HERE]);
return $client;
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
TBH i don't think service accounts even have refresh tokens.
@LindaLawton Thank you. It's not a big deal. But It use to be possible to check isAccessTokenExpired().
This is how i create my client
/**
* @return \Google_Client
* @throws \Google_Exception
*/
public function getClient()
{
if (empty($this->client)) {
$gmbConfig = $this->settings;
// check if json file exist
if (!$gmbConfig) {
throw new \Exception("Could not read environment configuration.");
}
$this->client = new \Google_Client([
'retry' => [
// this is how many times it will try after initial attempt
// total will be first attempt + retries (in this case 5 attempts)
'retries' => 4
]
]);
$this->client->setSubject($gmbConfig['userToImpersonate']);
$this->client->setScopes($gmbConfig['scope']);
$this->client->setAuthConfig($gmbConfig['authJson']);
$this->client->setAccessType("offline");
$this->client->setApprovalPrompt("force");
$this->apiUrl = $gmbConfig['apiUrl'];
}
return $this->client;
}
This token is only good for 360 seconds with "google/apiclient": "2.2.2" I could check for token expire and it would return false after an hour. Now it does not return true even after the token expires. Perhaps we were using isAccessTokenExpired() incorrectly and getting away with it. Until we updated the library.