Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
947 views
in Technique[技术] by (71.8m points)

php - 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.

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
", $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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...