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('[email protected]');
$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
$fileMetadata = new Google_Service_Drive_DriveFile(array(
'name' => 'New Test Folder',
'mimeType' => 'application/vnd.google-apps.folder',
'supportsAllDrives' => true,
'driveId' => '0AIuzzEYPQu9CUk9PVA'
));
UPDATE Attempt 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" } ]
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.
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.'supportsAllDrives' => true,
is part of the optional parameters and not part of the file metadata.
2. Both the parent
and the driveId
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',
'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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With