简体   繁体   English

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

[英]Why are the properties of Google_Service_Drive files only returning null?

I want to list all files in a folder shared with my Service Account.我想列出与我的服务帐户共享的文件夹中的所有文件。 I'm trying to figure out what files have a 'parent' attribute set to the folder ID or folder name.我试图找出哪些文件的“父”属性设置为文件夹 ID 或文件夹名称。 However, all the objects that I output show "NULL" for nearly all fields.但是,对于几乎所有字段,我输出的所有对象都显示“NULL”。 I've searched through the Google Drive API v3 and I can't even find the listFiles() function in it.我已经搜索过 Google Drive API v3,我什至找不到 listFiles() 函数。 Additionally, the documentation recommends using the Files.list function for outputting a list of files but when I use the Files.list function, I get an error as if the method doesn't exist.此外,文档建议使用 Files.list 函数输出文件列表,但是当我使用 Files.list 函数时,我收到一个错误,好像该方法不存在一样。 So I went back to using listFiles()... My code looks like this:所以我又回到使用 listFiles() ......我的代码如下所示:

public function getTitlesAndIDs($folderID)
{

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

    var_dump($files);
}

Trying to add capabilities just returns NULL as well.尝试添加功能也只会返回 NULL。 When I run listFiles() I get output like this:当我运行 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"]=>
...

Why are the values all coming back as NULL and how do I fix this so I can search by parent folders?为什么所有值都返回为 NULL,我该如何解决这个问题,以便我可以按父文件夹进行搜索? Is there a php equivalent to the Files.list function?是否有等同于 Files.list 函数的 php? Thanks in advance.提前致谢。

First you need to make sure that there are files that the service account has access to.首先,您需要确保服务帐户可以访问某些文件。 Once you have done so this should be able to list the files.完成后,这应该能够列出文件。

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

Code ripped from PHP QuickstartPHP 快速入门中提取的代码

除了@DalmTo 所说的之外,还有一件重要的事情要明确指出:您几乎所有字段都收到NULL ,因为您没有提供字段列表, 'fields'作为可选参数。

In addition to Google Drive API V3 properties such as id, name, kind and description , there are custom file attributes available to the developer via properties and appProperties .除了 Google Drive API V3 属性(例如id, name, kinddescription ,开发人员还可以通过propertiesappProperties使用自定义文件属性。

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

Google Drive properties key / value pairs are controlled and manipulated by your application. Google Drive properties键/值对由您的应用程序控制和操作。

Throughout Google Drive V3 documentation, the term "properties" also refers to metadata .在整个 Google Drive V3 文档中,术语“属性”也指的是metadata I found this very confusing.我发现这非常令人困惑。 https://developers.google.com/drive/api/v3/reference/files https://developers.google.com/drive/api/v3/reference/files

In Google Drive V2, there are copious examples of how to manipulate key / value properties using PHP, but in Google Drive V3 the properties API was significantly changed, and PHP code examples are very sparse.在 Google Drive V2 中,有大量关于如何使用 PHP 操作键/值properties示例,但在 Google Drive V3 中, properties API 发生了显着变化,并且 PHP 代码示例非常稀少。

By modifying the example code above, I was able to retrieve my customized properties.通过修改上面的示例代码,我能够检索我的自定义属性。

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

I wanted to share my extension of the original answer for those developers who land on this page looking for a PHP example of manipulating properties and appProperties in Google Drive V3.我想为那些登陆此页面寻找在 Google Drive V3 中操作propertiesappProperties的 PHP 示例的开发人员分享我对原始答案的扩展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM