Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
978 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('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

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...