通过服务帐户在共享驱动器上创建文件/文件夹

时间:2020-01-13 17:39:45

标签: google-drive-api

我正在使用G-Suite Google云端硬盘和服务帐户连接到共享硬盘。我正在使用PHP API库。之前,我已经使用“常规” Google驱动器进行了这项工作,但是我们正在使用共享驱动器(也称为Team Drive)

我可以使用files-> listFiles()获取文件列表,方法是:

    $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);

这有效,并为我提供了我要求的特定文件/文件夹的ID。

但是,尝试在驱动器的顶部文件夹中创建一个文件夹会导致404错误,表示找不到文件/文件夹,即使我刚刚从文件/文件夹中检索了正确的ID。

        $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'));

我尝试了各种排列方式,但始终得到404。我已多次验证父级文件ID正确。我尝试使用teamdriveid,driveid和其他各种选项。

这是我发送的数据以及我从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."
  }
}

此过程适用于“常规”驱动器,但在共享驱动器上失败。

任何人都知道缺少什么?

1 个答案:

答案 0 :(得分:2)

您应在请求正文中提供supportsAllDrives,如您指定的here一样,将其作为查询参数提供。此外,不需要supportsTeamDrivesteamDriveId(已弃用)。所以你应该改变这个:

$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而不是作为参数,因此无法访问共享驱动器。

我希望这会有所帮助。