【发布时间】:2018-02-12 07:22:00
【问题描述】:
我正在使用 PHP 开发 google drive api。使用此驱动器 api 时遇到错误。我正在使用 google api 客户端库“google/apiclient ^2.0”。我正在尝试使用谷歌身份验证用户登录并查看驱动器中所有可用的文件,我正在使用这个库,但它显示错误:
注意:未定义的属性:Google_Client::$files in /Applications/XAMPP/xamppfiles/htdocs/google_app/index.php 在第 31 行
致命错误:未捕获的错误:调用成员函数 listFiles() on /Applications/XAMPP/xamppfiles/htdocs/google_app/index.php:31 中为空 堆栈跟踪:#0 /Applications/XAMPP/xamppfiles/htdocs/google_app/index.php(55): retrieveAllFiles(Object(Google_Client)) #1 {main} 抛出 /Applications/XAMPP/xamppfiles/htdocs/google_app/index.php 在第 31 行
include_once 'vendor/autoload.php';
function retrieveAllFiles($service) {
$result = array();
$pageToken = NULL;
do {
try {
$parameters = array();
if ($pageToken) {
$parameters['pageToken'] = $pageToken;
}
$files = $service->files->listFiles($parameters);
$result = array_merge($result, $files->getItems());
$pageToken = $files->getNextPageToken();
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
$pageToken = NULL;
}
} while ($pageToken);
return $result;
}
$client = new Google_Client();
$client->setAuthConfig('client_secrets.json');
$client->setAccessType("offline"); // offline access
//$client->setIncludeGrantedScopes(true); // incremental auth
$client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY);
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
$client->setRedirectUri($redirect_uri);
if (isset($_GET['code'])) {
$token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
}
$service = new Google_service_Device();
echo retrieveAllFiles($service );
实际上,我正在尝试使用我的文件 ID 检索我的谷歌驱动器中的所有文件,然后设置状态自动发布特定文件。请帮我摆脱这个错误。
提前致谢。
【问题讨论】:
-
错误解释得很清楚。你试过阅读错误吗?
-
现在出现新错误“超出未经身份验证的使用的每日限制”?为什么会这样?我只尝试我的代码 4 或 5 次。我怎样才能摆脱这个错误
标签: php google-api google-drive-api google-oauth google-api-php-client