I'm uploading a zip file to a shared drive but since I don't want it to be duplicated before uploading it I'm trying to delete it.
The upload works fine. But the delete results in an error:
Uncaught Google\Service\Exception: { “error”: { “code”: 403, “message”: “The user does not have sufficient permissions for this file.“, “errors”: [ { “message”: “The user does not have sufficient permissions for this file.“, “domain”: “global”, “reason”: “insufficientFilePermissions” } ] } }
I'm the administrator of the shared drive so I have all the permission.
Here is my code:
require '../vendor/autoload.php';
if(session_status() == PHP_SESSION_NONE)
session_start();
$client = new Google_Client();
// credenziali google e scope
$client->setClientId('client id');
$client->setClientSecret('client secret');
$client->setRedirectUri('redirect puri');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
if (isset($_GET['code']) || (isset($_SESSION['access_token']) && $_SESSION['access_token'])) {
if (isset($_GET['code'])) {
$client->fetchAccessTokenWithAuthCode($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
}
else
$client->setAccessToken($_SESSION['access_token']);
$service = new Google_Service_Drive($client);
//HERE WAS THE CODE TO GET THE NAME OF THE FILE
// ----
//check if file exists
$optParams = array(
'pageSize' => 1000,
'supportsAllDrives' => true,
'includeItemsFromAllDrives' => true,
'driveId' => '***', // id of shared drive
'corpora' => 'drive',
'fields' => 'files(id, name)',
'q' => "name='" . $name . "2023.zip' and trashed = false"
);
$results = $service->files->listFiles($optParams);
// delete latest version already on drive
if(count($results->getFiles()) == 1) {
$service->files->delete($results->getFiles()[0]->getId(), array('supportsAllDrives' => true));
}
//upload on drive of the new version
$file = new Google_Service_Drive_DriveFile();
$file->setName($name); // name of the file
$file->setMimeType('application/zip'); // type
$file->setParents(['***']); // id of shared drive
$data = file_get_contents("path"); // path to file (hidden for the question)
$createdFile = $service->files->create($file, array(
'data' => $data,
'mimeType' => 'application/zip',
'uploadType' => 'multipart',
'supportsAllDrives' => true
));
} else {
// name saving
file_put_contents('code.txt', $_GET['name']);
$authUrl = $client->createAuthUrl();
header('Location: ' . $authUrl);
exit();
}
I see what is the issue, I tested deleting a file from a Shared Drive as a Content manager
and I get the same error you mentioned, I'm only able to delete the file from the Google Drive UI.
Reading through the documentation for the files.delete
method, it says:
If the file belongs to a shared drive the user must be an organizer on the parent.
So this means that through the Drive API we're not able to delete files as Content manager
, the currently authenticated user must be an organizer on the parent for the Shared Drive file.
The documentations says that as a Content manager
you can "By default, can upload, edit, move, or delete all files." but it seems this will only work if you do it through the Google Drive UI.
If you want to provide a feedback regarding this behavior, you can submit it through here.