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.