php - 为什么 Google_Service_Drive 文件的属性只返回 null?

标签 php google-api google-drive-api google-api-php-client service-accounts

我想列出与我的服务帐户共享的文件夹中的所有文件。我试图弄清楚哪些文件的“父”属性设置为文件夹 ID 或文件夹名称。但是,我输出的所有对象几乎所有字段都显示“NULL”。我已经搜索过 Google Drive API v3,但我什至无法在其中找到 listFiles() 函数。此外,文档建议使用 Files.list 函数输出文件列表,但是当我使用 Files.list 函数时,我收到一个错误,好像该方法不存在一样。所以我又开始使用 listFiles()...我的代码如下所示:

public function getTitlesAndIDs($folderID)
{

    // $capabilities = new \Google_Service_Drive_DriveFileCapabilities();
    // $capabilities->canListChildren = true;
    $files = $this->driveService->files->listFiles();

    var_dump($files);
}

尝试添加功能也只会返回 NULL。当我运行 listFiles() 时,我得到如下输出:

["files"]=>
  array(100) {
    [0]=>
    object(Google_Service_Drive_DriveFile)#77 (65) {
      ["collection_key":protected]=>
      string(6) "spaces"
      ["appProperties"]=>
      NULL
      ["capabilitiesType":protected]=>
      string(42) "Google_Service_Drive_DriveFileCapabilities"
      ["capabilitiesDataType":protected]=>
      string(0) ""
      ["contentHintsType":protected]=>
      string(42) "Google_Service_Drive_DriveFileContentHints"
      ["contentHintsDataType":protected]=>
      string(0) ""
      ["createdTime"]=>
      NULL
      ["description"]=>
      NULL
      ["explicitlyTrashed"]=>
      NULL
      ["fileExtension"]=>
      NULL
      ["folderColorRgb"]=>
...

为什么所有值都返回为 NULL,我该如何解决这个问题以便我可以按父文件夹搜索?是否有等效于 Files.list 函数的 php? 提前致谢。

最佳答案

首先,您需要确保服务帐户可以访问某些文件。一旦你这样做了,这应该能够列出文件。

$service = new Google_Service_Drive($client);

// Print the names and IDs for up to 10 files.
$optParams = array(
  'pageSize' => 10,
  'fields' => 'nextPageToken, files(id, name)'
);
$results = $service->files->listFiles($optParams);

if (count($results->getFiles()) == 0) {
  print "No files found.\n";
} else {
  print "Files:\n";
  foreach ($results->getFiles() as $file) {
    printf("%s (%s)\n", $file->getName(), $file->getId());
  }
}

PHP Quickstart 中提取的代码

关于php - 为什么 Google_Service_Drive 文件的属性只返回 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46303483/

相关文章:

android - Geocoder.getFromLocation() 以阿拉伯语返回地址

.net - 用于 Web 表单的 Google.Apis.Auth.OAuth

python - Google Drive API 权限,如何禁用互联网上任何人都可以找到的功能?

javascript - 使用 javascript 检查 URL 是否包含值

php - 无法查看/删除文件 PHP

Python - 通过 Google Sheet API 创建 Google Sheet 并将其发布到网站

c# - 使用 Google Drive API 下载文件

html - 如何在 iframe 中查看 Google drive pdf 链接

javascript - 如何在不重新加载页面的情况下对数据进行分页?

php - 从wordpress中的摘录末尾删除3个点