How can I create a folder within a Team Drive with

2019-02-23 09:04发布

I'm struggling to create a folder within a Team Drive using the Google API PHP Client Library.

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. However, when I create a folder it always creates it in 'My Drive' rather than the Team Drive specified.

Attempt 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',
    'supportsTeamDrives' => true,
    'parents' => ['0AIuzzEYPQu9CUk9PVA']
));

Attempt 2

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

UPDATE Attempt 3

$fileMetadata = new Google_Service_Drive_DriveFile(array(
    'name' => 'Hello 123',
    'supportsTeamDrives' => 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" } ]

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.

The folder is created correctly, just in the wrong place.

If anyone has any ideas I'd appreciate the help.

Thanks

1条回答
女痞
2楼-- · 2019-02-23 09:15

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:

1.'supportsTeamDrives' => true, is part of the optional parameters and not part of the file metadata. 2. Both the parent and the teamDriveId should be included as part of the metadata

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',    
    'teamDriveId' => $parent,
    'parents' => array($parent)
));

$optParams = array( 
    'fields' => 'id', 
    'supportsTeamDrives' => 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.

查看更多
登录 后发表回答