简体   繁体   中英

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. However, all the objects that I output show "NULL" for nearly all fields. I've searched through the Google Drive API v3 and I can't even find the listFiles() function in it. 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. So I went back to using listFiles()... My code looks like this:

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. When I run listFiles() I get output like this:

["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? Is there a php equivalent to the Files.list function? 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 Quickstart

除了@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 .

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

Google Drive properties key / value pairs are controlled and manipulated by your application.

Throughout Google Drive V3 documentation, the term "properties" also refers to metadata . I found this very confusing. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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