![](/img/trans.png)
[英]Google_Service_Drive retrieveListOfAllFiles only returning one file
[英]Why are the properties of Google_Service_Drive files only returning null?
我想列出與我的服務帳戶共享的文件夾中的所有文件。 我試圖找出哪些文件的“父”屬性設置為文件夾 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());
}
}
除了@DalmTo 所說的之外,還有一件重要的事情要明確指出:您幾乎所有字段都收到NULL
,因為您沒有提供字段列表, 'fields'
作為可選參數。
除了 Google Drive API V3 屬性(例如id, name, kind
和description
,開發人員還可以通過properties
和appProperties
使用自定義文件屬性。
https://developers.google.com/drive/api/v3/properties
Google Drive properties
鍵/值對由您的應用程序控制和操作。
在整個 Google Drive V3 文檔中,術語“屬性”也指的是metadata 。 我發現這非常令人困惑。 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);
}
}
}
我想為那些登陸此頁面尋找在 Google Drive V3 中操作properties
和appProperties
的 PHP 示例的開發人員分享我對原始答案的擴展。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.