Google-api-php-client: Notice: "Undefined index: expires_in" when "access_type" is set to "offline"

Created on 31 Aug 2016  路  11Comments  路  Source: googleapis/google-api-php-client

My code uploads files to Google Drive without problems, but I'm getting this notice (because it is not controlled the offline apps in isAccessTokenExpired method).

Notice: Undefined index: expires_in in /var/www/public/api/vendor/google/apiclient/src/Google/Client.php on line 470

Most helpful comment

When calling $client->setAccessToken(), make sure you are passing the entire json string, not only the token part. The json string contains the expires_in timestamp which is evaluated in isAccessTokenExpired

All 11 comments

When calling $client->setAccessToken(), make sure you are passing the entire json string, not only the token part. The json string contains the expires_in timestamp which is evaluated in isAccessTokenExpired

Thank you @baurma worked out for me. I was passing token alone for checking.

Thanks @baurma. I had the same issue as above and you really saved my day.

Thank you @baurma i was also passing the string alone.

@baurma I can't get it work by passing the JSON string to setAccessToken() method, if I try to use $client->getAccessToken() is only return the array containeing the access token

[
     "access_token" => "the token string....."
]

screenshot from 2017-08-30 10-32-56

This is the test() method from Drive class.

public function test() {
    return $this->client->getAccessToken();
}

and this is how i get the access token.

public function authenticate($data = []) {
    if(!is_array($data)) {
        return false;
    }

    $data = $this->client->fetchAccessTokenWithAuthCode($data['code']);
    file_put_contents($this->config['credential'], json_encode($data, JSON_PRETTY_PRINT));
    $this->client->setAccessToken(json_encode($data));
    return true;
}

the data array is auth code & scope from google after the OAuth complete & the $this->config['credential'] is the json file where I save the Access Token.

but.. this keep the isAccessTokenExpired() method not work.

screenshot from 2017-08-30 10-47-19

FYI I'm using Laravel 5.4 and google/apiclient library. Thanks for your help I'm very appreciated!

This code take from Google\Client.php

/**
   * Attempt to exchange a code for an valid authentication token.
   * Helper wrapped around the OAuth 2.0 implementation.
   *
   * @param $code string code from accounts.google.com
   * @return array access token
   */
  public function fetchAccessTokenWithAuthCode($code)
  {
    if (strlen($code) == 0) {
      throw new InvalidArgumentException("Invalid code");
    }

    $auth = $this->getOAuth2Service();
    $auth->setCode($code);
    $auth->setRedirectUri($this->getRedirectUri());

    $httpHandler = HttpHandlerFactory::build($this->getHttpClient());
    $creds = $auth->fetchAuthToken($httpHandler);
    if ($creds && isset($creds['access_token'])) {
      $creds['created'] = time();
      $this->setAccessToken($creds);
    }

    return $creds;
  }

these method has been set the access token, do we doesn't need to set the access token again???

@rizalio: Not sure. What I can imaging is that json_encode returns FALSE. Verify json_encode works. Instead of
$this->client->setAccessToken(json_encode($data));
try something like

if ($json = json_encode($data)) {
echo "json is $json\n";
$this->client->setAccessToken($json);
} else {
die("json_encode failed");
}

also verify that the string reported by "json is" contains all the required data.

@baurma I also think like that, but when I'm use the getAccessToken() method, the SDK return the sccess_token array that mean (maybe) the JSON is set successfully. But I'm not try it yet, Thanks for your help I'll try it!

Oops! My mistake :joy: I'm setting the setAccessToken() method on the __construct() with parameter only the access_token string :joy: because I want to set the \Google_Service_Drive that need the $client instance on my $service property :joy:

screenshot from 2017-08-30 15-55-21

Thanks for your help!

I have an access token and set this access token from my request.
after that, I have to fetch user object, Below error generate.

Notice: Undefined index: expires_in in opt/lampp/htdocs/project/vendor/google/apiclient/src/Google/Client.php

**here is my code.

    $client = new Google_Client();
    $client->setApplicationName(env('GOOGLE_APP_NAME'));
    $client->setClientId(env('GOOGLE_CLIENT_ID'));
    $client->setClientSecret(env('GOOGLE_CLIENT_SECRET'));
    $client->setRedirectUri('http://localhost:8000');
    $client->setDeveloperKey(env('GOOGLE_API_KEY'));
    $client->setAccessToken($request->authToken);
    $plus = new Google_Service_Plus($client);
    dd($plus->people->get('me'));**

Hi,

I faced the same issue. Running on local docker is ok, but failed on server.

I had to fix this way

line 500

    return $created <= 0 || $created > 0 && ($created + ($this->token['expires_in'] - 30)) < time();
Was this page helpful?
0 / 5 - 0 ratings

Related issues

kungufli picture kungufli  路  3Comments

Dresing picture Dresing  路  3Comments

artemiusgreat picture artemiusgreat  路  3Comments

mevsme picture mevsme  路  4Comments

camohub picture camohub  路  3Comments