简体   繁体   English

如何使用 Google API 在团队云端硬盘中创建文件夹?

[英]How can I create a folder within a Team Drive with the Google API?

I'm struggling to create a folder within a Team Drive using the Google API PHP Client Library.我正在努力使用 Google API PHP 客户端库在团队驱动器中创建一个文件夹。

I am using a service account and impersonating a user (myself) who is a member of the Team Drive and can list the contents of the drive.我正在使用一个服务帐户并模拟一个用户(我自己),该用户是 Team Drive 的成员并且可以列出该驱动器的内容。 However, when I create a folder it always creates it in 'My Drive' rather than the Team Drive specified.但是,当我创建一个文件夹时,它总是在“我的驱动器”而不是指定的团队驱动器中创建它。

Attempt 1尝试 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']
));

Attempt 2尝试 2

$fileMetadata = new Google_Service_Drive_DriveFile(array(
    'name' => 'New Test Folder',
    'mimeType' => 'application/vnd.google-apps.folder',
    'supportsAllDrives' => true,
    'driveId' => '0AIuzzEYPQu9CUk9PVA'
));

UPDATE Attempt 3更新尝试 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);

Attempt 3 gives this error: Fatal error: Uncaught Google_Service_Exception: { "error": { "errors": [ { "domain": "global", "reason": "notFound", "message": "File not found: 0AIuzzEYPQu9CUk9PVA.", "locationType": "parameter", "location": "fileId" } ]尝试 3 给出了这个错误:致命错误:未捕获的 Google_Service_Exception: { "error": { "errors": [ { "domain": "global", "reason": "notFound", "message": "File not found: 0AIuzzEYPQu9CUk9PVA .", "locationType": "参数", "location": "fileId" } ]

I have read all the (limited) documentation regarding Team Drive and the API and understand that a folder/file within a Team Drive can only have one parent (The Team Drive's ID) so I have tried variations of the parent as an array and string.我已阅读有关 Team Drive 和 API 的所有(有限)文档,并了解 Team Drive 中的文件夹/文件只能有一个父级(Team Drive 的 ID),因此我尝试将父级变体作为数组和字符串.

The folder is created correctly, just in the wrong place.该文件夹已正确创建,只是在错误的位置。

The documentation is not very clear on how to handle the creation of folders inside Teamdrives but this are the two things you need to take note of:关于如何在 Teamdrives 中创建文件夹的文档不是很清楚,但这是您需要注意的两件事:

1. 'supportsAllDrives' => true, is part of the optional parameters and not part of the file metadata. 1. 'supportsAllDrives' => true,是可选参数的一部分,而不是文件元数据的一部分。 2. Both the parent and the driveId should be included as part of the metadata 2. parentdriveId都应作为元数据的一部分包含在内

So here is an example on how to achieve this:所以这是一个关于如何实现这一点的例子:

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

Please Note: You will need the client library version 2.1.3 or greater.请注意:您将需要客户端库版本 2.1.3 或更高版本。

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

相关问题 如何使用PHP使用Google Drive API正确定义Google_Service_Drive变量来创建文件夹? - How can I define a Google_Service_Drive variable correctly with Google Drive API using PHP to create a folder? 使用 Google 云端硬盘 API 将文件夹移动到团队云端硬盘 - Move a folder into a Team Drive using Google Drive API 如何将文件上传到谷歌驱动器中的特定文件夹 - Google 驱动器 API (PHP) - How can i upload file to a specific folder in google drive -Google drive API (PHP) 如何通过驱动器 API 在谷歌驱动器中创建公共可访问文件夹? - How to create a public accessible folder in google drive through drive API? 如何检查Google Drive REST API中是否损坏了Team驱动器? - How to check if a Team drive is Trashed in Google Drive REST API? 使用 PHP 创建文件夹 Google Drive API - Create folder Google Drive API with PHP 如何显示 Google Drive 文件夹中的图像? - How can I show images from a Google Drive folder? 在根目录下创建文件夹并将文件上传到该文件夹​​google drive api - Create folder in root directiry of and upload file to that folder google drive api 如果文件夹中不存在文件,请删除文件夹 - Google Drive API - Delete folder if no files exist within it - Google Drive API 如何使用 Google drive api 和 PHP 从团队驱动器中删除用户 - How to remove user from team drive using Google drive api and PHP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM