首页 > 解决方案 > 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?

标签: google-drive-api

解决方案


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在请求正文中提供而不是作为参数提供,所以您无法访问共享驱动器。

我希望这有任何帮助。


推荐阅读