Php 为什么Google_服务_驱动器文件的属性只返回空值?
我想列出与我的服务帐户共享的文件夹中的所有文件。我试图找出哪些文件的文件夹ID或文件夹名称设置了“父”属性。但是,对于几乎所有字段,我输出的所有对象都显示为“NULL”。我已经搜索了Google Drive API v3,但在其中甚至找不到listFiles()函数。此外,文档建议使用Files.list函数输出文件列表,但当我使用Files.list函数时,会出现一个错误,就好像该方法不存在一样。所以我回到了使用listFiles()。。。我的代码如下所示:Php 为什么Google_服务_驱动器文件的属性只返回空值?,php,google-api,google-drive-api,google-api-php-client,service-accounts,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 g
public function getTitlesAndIDs($folderID)
{
// $capabilities = new \Google_Service_Drive_DriveFileCapabilities();
// $capabilities->canListChildren = true;
$files = $this->driveService->files->listFiles();
var_dump($files);
}
["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。当我运行listFiles()时,我得到如下输出:
public function getTitlesAndIDs($folderID)
{
// $capabilities = new \Google_Service_Drive_DriveFileCapabilities();
// $capabilities->canListChildren = true;
$files = $this->driveService->files->listFiles();
var_dump($files);
}
["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、,种类
和说明
,开发者可以通过属性
和应用属性
使用自定义文件属性
Google Drive属性
键/值对由应用程序控制和操作
在整个GoogleDriveV3文档中,术语“属性”也指元数据。我觉得这很令人困惑
在Google Drive V2中,有大量关于如何使用PHP操作键/值属性
的示例,但在Google Drive V3中,属性
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中操作
属性
和appProperties
的PHP示例的开发人员使用。它可能为空,因为您的服务帐户还没有任何文件。默认情况下,仅仅共享一个目录并不能让它访问该目录中的文件。此外,没有列出您需要执行的所有操作。请先列出一个file.list,然后对任何其他页面的数据进行分页。谢谢您的评论。目录中确实有文件。我还亲自查看了每个文档,并将其与服务帐户共享。Php不使用“.”符号。它使用文件->列表。。。但api中“files”参数“list”下没有函数。只有listFiles()无法完成我需要它完成的任务。这帮助我找到了答案。对于任何其他可能发现这个问题的人。。。显然,在PHP中,Files.list函数“是”listFiles()函数,尽管后者的名称没有出现在前者的页面上。这对我来说是正确的答案!我在查询中忘记了这个参数,示例代码只请求id和名称。如果不输入任何其他字段(如我的例子中的“trashed”),其值将始终为空。谢谢