google-drive-api - Create file/folder on Shared drive via service account
问题描述
I'm using a G-Suite Google Drive and Service account to connect to a Shared Drive. I'm using the PHP API library. I have previously made this work with the "regular" Google drive but we are moving to shared drive (aka Team Drive)
I can use files->listFiles() to get the file list, using:
$options = array('pageSize' => 100,
'corpora' => 'drive',
'supportsTeamDrives' => true,
'includeTeamDriveItems' => true,
'teamDriveId' => $sharedID,
'fields' => "nextPageToken, files(id, name)",
'q' => "'" . $pParentFolderID . "' in parents "
. " and name = \"" . $pFolderName . "\" "
. " and mimeType = 'application/vnd.google-apps.folder' "
. " and not trashed"
);
try{
$files_list = $this->drive_service->files->listFiles($optParams);
This works and gives me the ID for the specific file/folder I ask for.
However, attempting to create a folder in the top folder on the drive results in 404 error, saying that the file/folder is not found, even though I have just retrieved the correct ID from it.
$param =array( 'supportsAllDrives' => true,
'supportsTeamDrives' => true,
'teamDriveId' => $SharedID,
'parents' => array($pParentFolderID),
'name' => $pFolderName,
'mimeType' => 'application/vnd.google-apps.folder');
$fileMetadata = new Google_Service_Drive_DriveFile($param);
try {
$folderObj = $this->drive_service->files->create($fileMetadata, array('fields' => 'id'));
I have tried all kinds of permutations of this, but always get the 404. I have verified many times that the file ID for the parent is correct. I have tried using teamdriveid, driveid and various other options.
This is the data that I send and the data I get back from Google:
array(6) {
["supportsAllDrives"]=>
bool(true)
["supportsTeamDrives"]=>
bool(true)
["teamDriveId"]=>
string(19) "0ALay-iFeEOX6Uk9PVA"
["parents"]=>
array(1) {
[0]=>
string(33) "1oI72fkTb-AObYWBEOI-ykS5eDYg3L2ar"
}
["name"]=>
string(9) "FieldTest"
["mimeType"]=>
string(34) "application/vnd.google-apps.folder"
}
Trying to create:
Error from Google:array(1) {
["error"]=>
array(3) {
["errors"]=>
array(1) {
[0]=>
array(5) {
["domain"]=>
string(6) "global"
["reason"]=>
string(8) "notFound"
["message"]=>
string(50) "File not found: 1oI72fkTb-AObYWBEOI-ykS5eDYg3L2ar."
["locationType"]=>
string(9) "parameter"
["location"]=>
string(6) "fileId"
}
}
["code"]=>
int(404)
["message"]=>
string(50) "File not found: 1oI72fkTb-AObYWBEOI-ykS5eDYg3L2ar."
}
}
This process works with the "regular" drive, but fails on the Shared Drive.
Anyone have an idea what is missing?
解决方案
supportsAllDrives
当您应该将其作为查询参数提供时,您在请求正文中提供,如此处所指定。此外,supportsTeamDrives
并且teamDriveId
不需要(并且已弃用)。所以你应该改变这个:
$param =array( 'supportsAllDrives' => true,
'supportsTeamDrives' => true,
'teamDriveId' => $SharedID,
'parents' => array($pParentFolderID),
'name' => $pFolderName,
'mimeType' => 'application/vnd.google-apps.folder');
对此:
$param =array( 'parents' => array($pParentFolderID),
'name' => $pFolderName,
'mimeType' => 'application/vnd.google-apps.folder');
另外,正如我所说,您应该supportsAllDrives
在拨打电话时提供参数。所以你应该改变这个:
$folderObj = $this->drive_service->files->create($fileMetadata, array('fields' => 'id'));
对此:
$folderObj = $this->drive_service->files->create($fileMetadata, array('fields' => 'id', 'supportsAllDrives' => true));
因为您supportsAllDrives
在请求正文中提供而不是作为参数提供,所以您无法访问共享驱动器。
我希望这有任何帮助。
推荐阅读
- android - 底部工作表对话框片段已关闭,无法再次显示
- ssh - *** 检测到缓冲区溢出 *** /usr/bin/expect 终止
- github - 在 CircleCI 中安装 GitHub 包
- sql - SQL如何删除重复记录
- machine-learning - 训练+测试集是否必须与预测集不同(以便您需要对所有列应用时移)?
- python-3.x - 为什么熊猫对象的副本会更改原始对象上的一列?(切片副本)
- c# - 是否有一种简单的方法可以在 C# 中将断开事件捕获到 NATS 连接
- php - Laravel imap 获取按最近排序的消息
- angular - Angular - VM Clarity datagrid onscroll 事件
- c# - 如何在 C# 中使用 REST API 获取 Azure Batch 池和作业的列表?