我正在努力使用 Google API PHP 客户端库在团队云端硬盘中创建文件夹。
我正在使用服务帐户并冒充作为团队驱动器成员的用户(我自己),并且可以列出驱动器的内容。但是,当我创建文件夹时,它总是在“我的云端硬盘”而不是指定的团队云端硬盘中创建。
尝试 1
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope("https://www.googleapis.com/auth/drive");
$client->setSubject('user@mydomain.com');
$service = new Google_Service_Drive($client);
$folderId = '0AIuzzEYPQu9CUk9PVA';
$fileMetadata = new Google_Service_Drive_DriveFile(array(
'name' => 'New Test Folder',
'mimeType' => 'application/vnd.google-apps.folder',
'supportsAllDrives' => true,
'parents' => ['0AIuzzEYPQu9CUk9PVA']
));
尝试 2
$fileMetadata = new Google_Service_Drive_DriveFile(array(
'name' => 'New Test Folder',
'mimeType' => 'application/vnd.google-apps.folder',
'supportsAllDrives' => true,
'driveId' => '0AIuzzEYPQu9CUk9PVA'
));
更新尝试 3
$fileMetadata = new Google_Service_Drive_DriveFile(array(
'name' => 'Hello 123',
'supportsAllDrives' => true,
'mimeType' => 'application/vnd.google-apps.folder',
'parents' => ['0AIuzzEYPQu9CUk9PVA']
));
$file = $service->files->create($fileMetadata, array(
'fields' => 'id'));
printf("Folder ID: %s\n", $file->id);
尝试 3 出现此错误: fatal error :未捕获的 Google_Service_Exception:{“错误”:{“错误”:[{“域”:“全局”,“原因”:“notFound”,“消息”:“找不到文件:0AIuzzEYPQu9CUk9PVA。”,“locationType” : "参数", "位置": "文件ID"} ]
我已阅读所有(有限)documentation关于 Team Drive 和 API,并了解 Team Drive 中的文件夹/文件只能有一个父级(Team Drive 的 ID),因此我尝试了父级的变体作为数组和字符串。
文件夹创建正确,只是位置错误。
最佳答案
关于如何在 Teamdrives 中创建文件夹的文档不是很清楚,但这是您需要注意的两件事:
1.'supportsAllDrives' => true,
是可选参数的一部分,而不是文件元数据的一部分。
2. parent
和 driveId
都应该作为元数据的一部分包含在内
所以这是一个关于如何实现这一点的例子:
$service = new Google_Service_Drive($client);
$parent = "0AA3C8xRqwerLglUk9PVA"; //Teamdrive ID
//Create new folder
$file = new Google_Service_Drive_DriveFile(array(
'name' => 'Test Folder',
'mimeType' => 'application/vnd.google-apps.folder',
'driveId' => $parent,
'parents' => array($parent)
));
$optParams = array(
'fields' => 'id',
'supportsAllDrives' => true,
);
$createdFile = $service->files->create($file, $optParams);
print "Created Folder: ".$createdFile->id;
请注意:您需要客户端库版本 2.1.3 或更高版本。
关于php - 如何使用 Google API 在团队云端硬盘中创建文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45327769/