Google-api-php-client: PHP Notice: Undefined index: expires_in

Created on 30 Nov 2017  Â·  5Comments  Â·  Source: googleapis/google-api-php-client

On this line:

https://github.com/google/google-api-php-client/blob/master/src/Google/Client.php#L480

// If the token is set to expire in the next 30 seconds.
return ($created + ($this->token['expires_in'] - 30)) < time();

Here is my init code:

$client = new \Google_Client(
            array(
                'client_id'     => $this->gmail['client_id'],
                'client_secret' => $this->gmail['client_secret'],
                'redirect_uris' => array(
                    Auth::get_plugin_auth_url(),
                ),
            )
        );
$client->setAccessType( 'offline' );
$client->setApprovalPrompt( 'force' );
$client->setIncludeGrantedScopes( false );
$client->addScope( \Google_Service_Gmail::GMAIL_SEND );
$client->setRedirectUri( self::get_plugin_auth_url() );

If $client->setIncludeGrantedScopes( true ); - no notice, as response is an array with expires_in key.

p1 bug

Most helpful comment

Same here. I stored the original access and refresh token in the database. While using them later the response does not contain the required expires_in index.

Tested on v0.42.

EDIT:
I managed to fix the issue by storing the properties "created" and "expires_in" in addition to the "access_token" and "refresh_token" after the initial request:

$foo->setAccessToken($result['access_token']);
$foo->setRefreshAccessToken($result['refresh_token']);
$foo->setAccessTokenCreated($result['created']);
$foo->setAccessTokenExpiresIn($result['expires_in']);

On the next request I create an array containing not only the "access_token" (like before), but also the properties "created" and "expires_in" as stored in the above snipped:

$this->_accessToken = [
    'access_token' => $foo->getAccessToken(),
    'created' => $foo->getAccessTokenCreated(),
    'expires_in' => $foo->getAccessTokenExpiresIn(),
];

$this->_client->setAccessToken($this->_accessToken);

I my case it was an error in my implementation, the api is working as expected.

solved

All 5 comments

isAccessTokenExpired is testing value usually passed in by $client->setAccessToken(…). Make sure you are passing the entire JSON string, not only the token part.

Typically this string is either fetched from the server $client->fetchAccessTokenWithAuthCode(…) or retrieved from a cache on disk json_decode(file_get_contents($credentialsPath), true).

Of course, I was doing exactly that. But for some reason got a response without expires_in, saved it in DB to reuse later, and passed to setAccessToken() which uses the array keys blindly.
My main point was that when I use legitimately the API with $client->setIncludeGrantedScopes( false );, the $creds array fron fetch...() method didn't contain the expires_in key, which is required in other parts of your library.

Same here. I stored the original access and refresh token in the database. While using them later the response does not contain the required expires_in index.

Tested on v0.42.

EDIT:
I managed to fix the issue by storing the properties "created" and "expires_in" in addition to the "access_token" and "refresh_token" after the initial request:

$foo->setAccessToken($result['access_token']);
$foo->setRefreshAccessToken($result['refresh_token']);
$foo->setAccessTokenCreated($result['created']);
$foo->setAccessTokenExpiresIn($result['expires_in']);

On the next request I create an array containing not only the "access_token" (like before), but also the properties "created" and "expires_in" as stored in the above snipped:

$this->_accessToken = [
    'access_token' => $foo->getAccessToken(),
    'created' => $foo->getAccessTokenCreated(),
    'expires_in' => $foo->getAccessTokenExpiresIn(),
];

$this->_client->setAccessToken($this->_accessToken);

I my case it was an error in my implementation, the api is working as expected.

solved

It looks like a member of our great community answered your questions. Thanks Community! Please add a comment if you have any more questions and we can reopen this issue.

that worked for me! I was passing the token as a string than the json which included the refresh etc

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Dresing picture Dresing  Â·  3Comments

ysaurabh33 picture ysaurabh33  Â·  3Comments

unixkapl picture unixkapl  Â·  3Comments

kungufli picture kungufli  Â·  3Comments

upendtu picture upendtu  Â·  4Comments