google-api-php-client
google-api-php-client copied to clipboard
isAccessTokenExpired is return false
Environment details
- OS: Mac OS
- PHP version: 7.4.14
- Package name and version: v2.12.1
Steps to reproduce
- I am using SearchConsole urlInspection API. I want to get the urlInspection for multiple URIs so I want to make check accessToken is valid or not, If valid then no need to generateAccess token again. If it is expired then before making API call will generate new access Token.
- If I set accesstoken as an array with created time it will fail with authentication
- I want to check accessToken is expired or not
Code example
//$accesstoken is string something like this:
//ya29.A0ARrdaM_-3jC.....
$client->setAccessToken($accesstoken);
if ($client->isAccessTokenExpired()) {
$output = $client->refreshToken($refresh_token);
//Get accesstoken and store New Token
$newtoken = $client->getAccessToken();
$client->setAccessToken($newtoken);
}
//If token generated then call the
$searchconsoleService = new Google\Service\SearchConsole($client);
Gives me error
Severity: Notice Message: Undefined index: expires_in Filename: /vendor/google/apiclient/src/Client.php Line Number: 574
Thanks!
The issue solved, please close it @dwsupplee @chiragvels
I have updated my code with same. But i am always getting $client->isAccessTokenExpired() to 1 means token showing expired every time.
When token just updated.
$client->setAccessToken($accessToken);
if ($client->isAccessTokenExpired()) {
if($refreshToken){
$token_response = $client->fetchAccessTokenWithRefreshToken($refreshToken);
//Update Access Token
if(!empty($token_response)){
$access_token = $token_response['access_token'];
$refresh_token = $token_response['refresh_token'];
$created = $token_response['created'];
$token_type = $token_response['token_type'];
$id_token = $token_response['id_token'];
$expires_in = $token_response['expires_in'];
updateAccessTokenToFile();
}
$client->setAccessToken($access_token);
} else {
throw new Exception('No valid Google token found');
}
}
Hi @chiragvels , sorry for this focus on this issue.
Can you tell me if this is still relevant or not?
Yes, relevant. It is still there for me.
Please help me understand couple of my questions so that I gain more understanding of the situation.
- How are you fetching the
$accessToken
and$refreshToken
? or alternatively, what type of credentials are you using? - Once credentials are set, whenever the
$client->execute()
method is invoked, this library internally takes care of the token refreshing logic.Client::execute()
code pointer ->Client::authorize()
code pointer. Is that working?
Hi @yash30201 ,
sorry for the delay. I am on holiday with limited accessibility. I will check your points and reply back soon.
Thanks
Closing due to inactivity, please reopen if the issue still persists.
Hi @yash30201 ,
- How are you fetching the $accessToken and $refreshToken? or alternatively, what type of credentials are you using?
- I am using accesstoken with offline access. And refreshtoken based on accestoken.
Get access token using this URI
$uri = "https://accounts.google.com/o/oauth2/v2/auth?client_id=$client_id&redirect_uri=$redirect_uri&scope=$scopes&response_type=code&access_type=offline&prompt=consent&state=$params"
And new access token using refreshtoken
$client->setAccessToken($accesstoken);
if ($client->isAccessTokenExpired()) {
$output = $client->refreshToken($refresh_token);
//Get accesstoken and store New Token
$newtoken = $client->getAccessToken();
$client->setAccessToken($newtoken);
}
- Once credentials are set, whenever the $client->execute() method is invoked, this library internally takes care of the token refreshing logic. Client::execute() code pointer -> Client::authorize() code pointer. Is that working?
- No this is not working with search console API, currently I tested.