使用 API 在 Google 共享云端硬盘中创建文件夹时"File not found"问题



我正在尝试使用google驱动器API v2.1.3在共享驱动器中创建一个文件夹。我的脚本在我的个人驱动器中创建文件夹的效果非常好,但一旦我放入共享驱动器的标识符,我就会不断地得到一个";找不到文件"。。。我指定我已在共享驱动器中为我的服务帐户授予权限。。。

我的代码:

$client = new Google_Client();
$client->setAuthConfig($chemin_idendifiant_google_drive_api);
$client->addScope('https://www.googleapis.com/auth/drive');
$service = new Google_Service_Drive($client);
$parent = '1-FzaEgtKFZ6XXXXXXXXXXkjcbtiDKZEQ'; //Folder in my shared drive
//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',
);
try {
$createdFile = $service->files->create($file, $optParams);
print "Created Folder: ".$createdFile->id;
} catch (Exception $e) {
echo 'Exception reçue : ',  $e->getMessage(), "n";
}

我已经读到;supportsAllDrives;应该使用属性,但在文档中注意到,自2020年6月1日以来,该属性已被弃用(https://developers.google.com/resources/api-libraries/documentation/drive/v3/php/latest/class-Google_Service_Drive_Files_Resource.html#_create)

感谢帮助:(

在您的脚本中,以下修改如何?

发件人:

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

收件人:

$file = new Google_Service_Drive_DriveFile(array(
'name' => 'Test Folder',
'mimeType' => 'application/vnd.google-apps.folder',
'parents' => array($parent)
));
$optParams = array(
'supportsAllDrives' => true,
'fields' => 'id',
);

注:

  • 当您没有权限作为写入程序访问$parent的文件夹时,会发生错误。请小心

参考:

  • 文件:创建

相关内容

最新更新