未定义的索引:expires_in/vendor/google/apiclient/src/client.php,574



我正在使用谷歌api,并面临以下错误-

未定义的索引:574中的expires_in/vendor/google/apiclient/src/client.php。

当我检查文件时,我不确定这个错误意味着什么

if (!isset($this->token['expires_in'])) {
// if the token does not have an "expires_in", then it's considered expired
return true;
}
// If the token is set to expire in the next 30 seconds.
return ($created + ($this->token['expires_in'] - 30)) < time();

你能让我知道这个错误意味着什么,以及我如何解决它吗?

这是我的自定义代码-

if (file_exists($credentialsPath)) {
$accessToken = json_decode(file_get_contents($credentialsPath), true);
} else {
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:n%sn", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
if (!file_exists(dirname($credentialsPath))) {
mkdir(dirname($credentialsPath), 0700, true);
}
file_put_contents($credentialsPath, json_encode($accessToken));
}
$client->setAccessToken($accessToken);
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
}

我很好奇你为什么要用expire_in?为什么不直接检查$client;isAccessTokenExpired((?客户端库会告诉你它是否过期。它还考虑了时钟偏移。

示例:

if ($client->isAccessTokenExpired()) {
// Refresh the token if possible, else fetch a new one.
if ($client->getRefreshToken()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
...

如果你真的想检查它,试试!isset($this->token['expires_in']],因为它听起来还没有设置。对代币进行print_r($this->token)可能会对您的问题有所了解。

更新

大胆猜测试试这个,你可能在解析文件时遇到问题。

if (file_exists(TOKENPATH)) {
$userCredentials = json_decode(file_get_contents(TOKENPATH), true);
$client->setAccessToken($userCredentials['refresh_token']);
$client->refreshToken($userCredentials['refresh_token']);
$client->fetchAccessTokenWithRefreshToken($userCredentials['refresh_token']);
}

最新更新