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

时间:2017-09-19 14:46:19

标签: 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? 提前谢谢。

3 个答案:

答案 0 :(得分:1)

首先,您需要确保服务帐户有权访问的文件。完成后,这应该能够列出文件。

$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

答案 1 :(得分:1)

除了@DalmTo所说的,还有一个重要的事情要明确指出:几乎所有字段都收到了'fields',因为你没有提供字段列表,Private Sub Workbook_Open() UserForm1.Show vbModeless End Sub 作为可选参数。

答案 2 :(得分:1)

除了id, name, kinddescription之类的Google Drive API V3属性外,开发人员还可以通过propertiesappProperties使用自定义文件属性。

https://developers.google.com/drive/api/v3/properties

Google云端硬盘properties键/值对由您的应用程序控制和操纵。

在整个Google Drive V3文档中,术语“属性”也指元数据。我觉得这很混乱。 https://developers.google.com/drive/api/v3/reference/files

在Google Drive V2中,有很多示例说明了如何使用PHP来操作键/值properties,但是在Google Drive V3中,properties API发生了很大变化,PHP代码示例非常稀疏

通过修改上面的示例代码,我能够检索到自定义的属性。

$service = new Google_Service_Drive($client);

// Print the names, IDs and properties for up to 10 files.
$optParams = array(
  'pageSize' => 10,
  'fields' => 'nextPageToken, files(id, name, properties)'
);
$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", "name: ".$file->getName(), "ID: ".$file->getId());
      foreach ($file->getProperties() as $key => $value) {
        printf("%s%s\n","-key: ".$key,", value: ".$value);
    }
  }
}

我想为那些登陆此页面并在PHP示例中使​​用Google Drive V3操纵propertiesappProperties的开发人员分享原始答案的扩展名。