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

问题描述

我在这里遇到快速启动php脚本的问题:

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

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

错误开始:
$ b 注意:未定义的索引:\ google-api-php-client \src中的expires_in \Google\Client.php在线485



致命错误:带有消息'refresh token的未捕获异常'LogicException'必须传入或设置为setAccessToken的一部分'

错误结束: 我的假设是,访问令牌已保存在该文件格式不正确。



当前格式

ya29.CODE-oN_Bearer36001 / _ANOTHER-CODE- ANOTHER_ANOTHER_CODE

正如你所见,它不包含变量expires_in



出错了?我正在运行脚本,没有任何修改。

解决方案

我已经调试过了....编写它的人犯了一个错误,不要调用 json_encode ,然后将auth结果写入token.json文件。



您可以通过添加 json_encode 在第45行。



所以...

  file_put_contents($ credentialsPath,$ accessToken); 

...应该是:

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

我已经提交了反馈,所以希望它能被修复。



edit:同样的问题发生在同一个方法中的令牌刷新调用



edit2:这是我在Github讨论中的相关评论,以及Google:



我提出了以下几条建议:

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

$ / code>

而不是:

  //如果令牌过期,刷新令牌。 
if($ client-> isAccessTokenExpired()){
$ client-> refreshToken($ client-> getRefreshToken());
file_put_contents($ credentialsPath,$ client-> getAccessToken());
}


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

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:

Error start:

Notice: Undefined index: expires_in in \google-api-php-client\src\Google\Client.php on line 485

Fatal error: Uncaught exception 'LogicException' with message 'refresh token must be passed in or set as part of setAccessToken' in

Error end:

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

Current format:

ya29.CODE-oN_Bearer36001/_ANOTHER-CODE-ANOTHER_ANOTHER_CODE

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.

解决方案

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.

You can fix it by adding json_encode on line 45.

So...

file_put_contents($credentialsPath, $accessToken);

...should be:

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

Instead of:

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

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

09-03 19:20