Google-API-PHP 客户端问题,运行快速启动脚本时出错 [英] Issue with Google-API-PHP Client, getting error when running the quick start script

查看:16
本文介绍了Google-API-PHP 客户端问题,运行快速启动脚本时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am facing an issue with quickstart php script here: https://developers.google.com/drive/v2/web/quickstart/php

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

When I run the script first time, it executes perfectly and the access token is stored in a file called: drive-php-quickstart.json

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

When I run the script second time, it gives me the error:

错误开始:

注意:未定义索引:google-api-php-clientsrcGoogleClient.php 第 485 行中的 expires_in

Notice: Undefined index: expires_in in google-api-php-clientsrcGoogleClient.php on line 485

致命错误:在

错误结束:

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

My assumption is that access token been saved in the file is not in the right format.

当前格式:

ya29.CODE-oN_Bearer36001/_ANOTHER-CODE-ANOTHER_ANOTHER_CODE

ya29.CODE-oN_Bearer36001/_ANOTHER-CODE-ANOTHER_ANOTHER_CODE

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

As you can see, it does not contain the variable "expires_in"

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

Any suggestions where I am going wrong ? I am running the script as it is, with no modifications.

推荐答案

我已经调试过了.... 写的人犯了一个错误,在写auth结果之前没有调用json_encode到 token.json 文件.

I've debugged it.... The person who wrote it made a mistake by not calling json_encode before writing the auth result to the token.json file.

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

You can fix it by adding json_encode on line 45.

所以...

file_put_contents($credentialsPath, $accessToken);

...应该是:

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

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

I've submitted feedback so hopefully it'll be fixed.

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

edit: same issue happens for the token refresh call in that same method

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

edit2: Here's my related comment in a Github discussion and an answer from Google: https://github.com/google/google-api-php-client/issues/263#issuecomment-186557360

我提出了以下几点建议:

I suggested something along the following lines:

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());
}

这篇关于Google-API-PHP 客户端问题,运行快速启动脚本时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆