如何使用Google API在团队云端硬盘中创建文件夹? [英] How can I create a folder within a Team Drive with the Google API?

查看:188
本文介绍了如何使用Google API在团队云端硬盘中创建文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用一个服务帐户并冒用一个用户(我自己)谁是团队驱动器的成员,并且可以列出驱动器的内容。但是,当我创建一个文件夹时,它总是在我的云端硬盘中创建它,而不是在指定的团队云端硬盘中创建它。

尝试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'=>'新测试文件夹',
'mimeType'=>'application / vnd.google-apps.folder',
'supportsTeamDrives'=> true,
'parents'=> ['0AIuzzEYPQu9CUk9PVA']
));

尝试2

  $ fileMetadata = new Google_Service_Drive_DriveFile(array(
'name'=>'新测试文件夹',
'mimeType'=>'application / vnd.google-apps.folder' ,
'supportsTeamDrives'=> true,
'teamDriveID'=>'0AIuzzEYPQu9CUk9PVA'
));

更新尝试3

  $ fileMetadata = new Google_Service_Drive_DriveFile(array(
'name'=>'Hello 123',
'supportsTeamDrives'=> true,
'mimeType'=>'application / vnd.google-apps.folder',
'parents'=> ['0AIuzzEYPQu9CUk9PVA']
));
$ b $ file = $ service-> files-> create($ fileMetadata,array(
'fields'=>'id'));
printf(文件夹ID:%s \ n,$ file-> id);

尝试3给出此错误:
致命错误:未捕获Google_Service_Exception:{error: {errors:[{domain:global,reason:notFound,message:File not found:0AIuzzEYPQu9CUk9PVA。,locationType:parameter,location:fileId }]



我已阅读所有(有限),并且了解团队云端硬盘中的文件夹/文件只能有一个父项(团队云端硬盘的ID),因此我尝试过将父项的变体作为一个数组和字符串。



文件夹被正确创建,只是在错误的地方。



任何想法,我会感谢帮助。

谢谢 关于如何处理Teamdrives内创建文件夹的文档不是很清楚,但这是你的两件事需要注意:

<1> 'supportsTeamDrives'=> true,是可选参数的一部分,而不是文件元数据的一部分。
2. 父母 teamDriveId 应作为元数据的一部分包含在内



所以这里有一个例子来说明如何实现这一点:

  $ service = new Google_Service_Drive($客户端); 
$ parent =0AA3C8xRqwerLglUk9PVA; // Teamdrive ID

//创建新文件夹
$ file = new Google_Service_Drive_DriveFile(array(
'name'=>'测试文件夹',
'mimeType '=>'application / vnd.google-apps.folder',
'teamDriveId'=> $ parent,
'parents'=> array($ parent)
)) ;

$ optParams = array(
'fields'=>'id',
'supportsTeamDrives'=> true,
);

$ createdFile = $ service-> files-> create($ file,$ optParams);
打印Created Folder:。$ createdFile-> id;

请注意:您需要客户端库版本2.1.3或更大。


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

解决方案

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.

这篇关于如何使用Google API在团队云端硬盘中创建文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆