google-api-php-client icon indicating copy to clipboard operation
google-api-php-client copied to clipboard

isAccessTokenExpired is return false

Open chiragvels opened this issue 3 years ago • 5 comments

Environment details

  • OS: Mac OS
  • PHP version: 7.4.14
  • Package name and version: v2.12.1

Steps to reproduce

  1. 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.
  2. If I set accesstoken as an array with created time it will fail with authentication
  3. 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!

chiragvels avatar Feb 22 '22 11:02 chiragvels

The issue solved, please close it @dwsupplee @chiragvels

dmitryuk avatar Apr 07 '22 12:04 dmitryuk

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');
        }
    }

chiragvels avatar Jul 24 '23 13:07 chiragvels

Hi @chiragvels , sorry for this focus on this issue.

Can you tell me if this is still relevant or not?

yash30201 avatar May 02 '24 11:05 yash30201

Yes, relevant. It is still there for me.

chiragvels avatar May 02 '24 11:05 chiragvels

Please help me understand couple of my questions so that I gain more understanding of the situation.

  1. How are you fetching the $accessToken and $refreshToken? or alternatively, what type of credentials are you using?
  2. 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?

yash30201 avatar May 02 '24 13:05 yash30201

Hi @yash30201 ,

sorry for the delay. I am on holiday with limited accessibility. I will check your points and reply back soon.

Thanks

chiragvels avatar May 07 '24 17:05 chiragvels

Closing due to inactivity, please reopen if the issue still persists.

yash30201 avatar May 15 '24 11:05 yash30201

Hi @yash30201 ,

  1. 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);
        }
  1. 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.

chiragvels avatar May 16 '24 11:05 chiragvels