0

Im getting my google access token,

and I set it like

$client->setAccessToken($token)

This is working for me, and has been for a long time. Ive been using it for API calls and it works. However, I only recently started working on the fresh functionality of my app. And I ran into a problem/..

When I want to check if this token is expired by using isAccessTokenExpired.

$client->isAccessTokenExpired()

I get

undefined index : expires in

I googled around, and apparently you have to pass the whole entire JSON encoded string, into setAccessToken. Not JUST the token. (which is wierd, because why would just the token work for API calls?)

However, now when I do that...

$token = json_decode($usersToken);
$client->setAccessToken($token);

I get

Cannot use object of type stdClass as array

Then I tried it converting the stdClass to array

 $token = json_decode($usersToken, true);
 $client->setAccessToken($token);

And I get

  Invalid Token Format   Client.php line 433

What the hell do I have to pass into setAccessToken, to have it be valid???

And why does just passing the token work for API calls, but fail on the checking of expiry. (obviously because I need the whole object, not just token string) But then how do I do that??

This behaviour is wierd and counterintuitive.

Kylie
  • 11,421
  • 11
  • 47
  • 78
  • Can you please post the key names in the array generated by decoding your token. Don''t need the values just the key names. – Joseph_J Dec 26 '18 at 10:28
  • I looked in Client.php, and it seems to want access_token to be a field. I seem to have every field besides that. id_token is the only token like field I have – Kylie Dec 26 '18 at 22:48
  • Can you please post more code so we can have more to go off of. Also please post the code for authorizing your client. Also please post the keys for the array after you decode your .json file. There could be a variety of different things that could be the problem. Like your token is expired and you don't have a refresh token to get a new token. Your scope could be defined wrong. We just need some more clues. Thanks! – Joseph_J Dec 27 '18 at 05:38

1 Answers1

0

So based off of the information you have in your question I am going to make an assumption about your problem. If this does not give you any help I will revisit the question after you post more code for us to look at.

Your JSON file that contains the users token should look something like this after you decode it:

$token = array(

'access_token'    => 'token value',
'expires_in'      => 3600,
'scope'           => 'a url specifying scope',
'token_type'      => 'Bearer',
'created'         => 'a timestamp for created date',
'refresh_token'  => 'refresh token value' 

);

This array can be passed to $client->setAccessToken($token) as an array.

If you think just that your script is calling just for the actual access token then:

$client->setAccessToken($token['access_token']);

If you run $client->isAccessTokenExpired() and you get an error like the one you posted in your question, one might assume that your token is missing the expires_in field and the created field.

isAccessTokenExpired() will return a bool response. Notice that you do not pass anything to the function because the $client should already have the information it needs to test if the token is expired or not.

If your token does not have the values that I posted above you will have to obtain a new token with the appropriate scopes.

The general flow of getting a new token is:

Taken from Google Calendar API Quickstart Guide - PHP

// If there is no previous token or it's expired.
    if ($client->isAccessTokenExpired()) {
        // Refresh the token if possible, else fetch a new one.
        if ($client->getRefreshToken()) {
            $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
        } else {
            // Request authorization from the user.
            $authUrl = $client->createAuthUrl();
            printf("Open the following link in your browser:\n%s\n", $authUrl);
            print 'Enter verification code: ';
            $authCode = trim(fgets(STDIN));

            // Exchange authorization code for an access token.
            $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
            $client->setAccessToken($accessToken);

            // Check to see if there was an error.
            if (array_key_exists('error', $accessToken)) {
                throw new Exception(join(', ', $accessToken));
            }
        }
        // Save the token to a file.
        if (!file_exists(dirname($tokenPath))) {
            mkdir(dirname($tokenPath), 0700, true);
        }
        file_put_contents($tokenPath, json_encode($client->getAccessToken()));
    }

Make sure your client object is set correctly:

$scopes = implode(' ', 

array(Google_Service_Calendar::CALENDAR)

);

$client = new Google_Client();
$client->setAuthConfig('path to credentials');  
$client->addScope($scopes);
$client->setAccessType('offline');

Also id_token and access_token are not the same thing. You should read up on the difference between the two. Since you have not mentioned what you are trying to do or what methods you are using everything is still a guess.

id_token vs access_token

Hope that may give you some insight.

Joseph_J
  • 3,654
  • 2
  • 13
  • 22