未定义的索引:在574中使用谷歌应用程序接口,在/vendor/google/apiclient/src/client. php中过期

dauxcl2d  于 2022-10-30  发布在  PHP
关注(0)|答案(1)|浏览(93)

我正在使用谷歌API,并面临以下错误-
未定义的索引:在574中,在/供应商/谷歌/apiclient/src/client. php中过期。
我不知道这个错误意味着什么,当我检查它被写的文件-

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

您能告诉我这个错误是什么意思,以及我如何解决它吗?
这是我的自定义代码-

  1. if (file_exists($credentialsPath)) {
  2. $accessToken = json_decode(file_get_contents($credentialsPath), true);
  3. } else {
  4. $authUrl = $client->createAuthUrl();
  5. printf("Open the following link in your browser:\n%s\n", $authUrl);
  6. print 'Enter verification code: ';
  7. $authCode = trim(fgets(STDIN));
  8. $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
  9. if (!file_exists(dirname($credentialsPath))) {
  10. mkdir(dirname($credentialsPath), 0700, true);
  11. }
  12. file_put_contents($credentialsPath, json_encode($accessToken));
  13. }
  14. $client->setAccessToken($accessToken);
  15. if ($client->isAccessTokenExpired()) {
  16. $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
  17. file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
  18. }
knpiaxh1

knpiaxh11#

我很好奇你为什么要用expire_in?为什么不直接检查$client-〉isAccessTokenExpired()呢?客户端库会告诉你它是否过期。它也会考虑时钟偏差。
示例:

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

如果你真的想检查它,尝试!isset($this->token['expires_in']],因为它听起来像是还没有设置。

更新

大胆猜测尝试这个,你可能有问题解析你的文件。

  1. if (file_exists(TOKENPATH)) {
  2. $userCredentials = json_decode(file_get_contents(TOKENPATH), true);
  3. $client->setAccessToken($userCredentials['refresh_token']);
  4. $client->refreshToken($userCredentials['refresh_token']);
  5. $client->fetchAccessTokenWithRefreshToken($userCredentials['refresh_token']);
  6. }
展开查看全部

相关问题