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

isAccessTokenExpired seems to behave differently from v2.2 to 2.10

Open jmckay56 opened this issue 5 years ago • 3 comments

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"

jmckay56 avatar Oct 20 '21 19:10 jmckay56

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?

jmckay56 avatar Oct 20 '21 21:10 jmckay56

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 avatar Oct 27 '21 12:10 LindaLawton

@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.

jmckay56 avatar Oct 29 '21 23:10 jmckay56