【问题标题】:Issue with Google-API-PHP Client, getting error when running the quick start scriptGoogle-API-PHP 客户端问题,运行快速启动脚本时出错
【发布时间】:2016-03-26 20:25:40
【问题描述】:

我在这里遇到了快速启动 php 脚本的问题:https://developers.google.com/drive/v2/web/quickstart/php

当我第一次运行脚本时,它完美执行,并且访问令牌存储在一个名为:drive-php-quickstart.json 的文件中

当我第二次运行脚本时,它给了我错误:

错误开始:

注意:未定义索引:\google-api-php-client\src\Google\Client.php 第 485 行中的 expires_in

致命错误:在

错误结束:

我的假设是保存在文件中的访问令牌格式不正确。

当前格式:

ya29.CODE-oN_Bearer36001/_ANOTHER-CODE-ANOTHER_ANOTHER_CODE

如您所见,它不包含变量“expires_in”

我哪里出错了有什么建议吗?我按原样运行脚本,没有修改。

【问题讨论】:

  • 您到底在 App Engine 上运行什么?您指向的快速入门以及问题上的所有文本显然都表明您在本地运行,与 App Engine 无关。如果是这样,你能改变标签吗?如果不是,请编辑您的问题以阐明 App Engine 的位置?谢谢。
  • 谢谢亚历克斯。我已经做出了改变。是的,我目前正在本地执行,但我很快就会将其转移到 Google App Engine。
  • 我有确切的问题。无需修改即可运行脚本。第一次运行完美,第二次使用refresh token must be passed in or set as part of setAccessToken 退出。

标签: google-drive-api google-api-php-client


【解决方案1】:

我已经调试过了....编写它的人犯了一个错误,在将身份验证结果写入 token.json 文件之前没有调用json_encode

您可以通过在第 45 行添加 json_encode 来修复它。

所以...

file_put_contents($credentialsPath, $accessToken);

...应该是:

file_put_contents($credentialsPath, json_encode($accessToken));

我已提交反馈,希望能得到修复。

编辑:在同一方法中的令牌刷新调用也会出现同样的问题

edit2:这是我在 Github 讨论中的相关评论和 Google 的回答:https://github.com/google/google-api-php-client/issues/263#issuecomment-186557360

我提出了以下几点建议:

if ($client->isAccessTokenExpired()) {
    $refreshToken = $client->getRefreshToken();
    $client->refreshToken($refreshToken);
    $newAccessToken = $client->getAccessToken();
    $newAccessToken['refresh_token'] = $refreshToken;
    file_put_contents($credentialsPath, json_encode($newAccessToken));
}

代替:

// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
    $client->refreshToken($client->getRefreshToken());
    file_put_contents($credentialsPath, $client->getAccessToken());
}

【讨论】:

  • 谢谢!我也提交了反馈。虽然它似乎是第 47 行
  • @RoshanBhumbra 啊,是的,也许是 47 .. 这张 Github 票中有更多信息:github.com/google/google-api-php-client/issues/…
  • @Erfan - 我必须在你的 github 评论中应用你的 refresh_token 设置。您能在此处提及或在此处粘贴该 sn-p 吗?我觉得它很有用。
  • 谢谢@mederomuraliev,添加了:)
  • 甜蜜。实际上,我确实实现了它,它在sheets.googleapis.com-php-quickstart.json 文件中有refresh_token 节点,但在随后的调用中我得到Fatal error: Uncaught exception 'LogicException' with message 'refresh token must be passed in or set as part of setAccessToken'。有什么想法吗?
【解决方案2】:

Google 更新了他们的PHP Quickstart,改进了处理此问题的方法:

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

// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
}

【讨论】:

    猜你喜欢
    • 2016-12-03
    • 1970-01-01
    • 2016-04-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多