1
votes

I am unable to delete a folder (created by another person) even if I try to change the rights, I have a console application and the current authenticated user can do the following :

  1. create folders/files
  2. move files

Scopes:

'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/drive.file',
'https://www.googleapis.com/auth/drive.appdata',
'https://spreadsheets.google.com/feeds',

Tries:

1.Set permissions to the current user

public function deleteFolder(\Google_Service_Drive_DriveFile $folder)
{
    $permission = new \Google_Service_Drive_Permission();
    $permission->setRole( 'owner' );
    $permission->setType( 'user' );
    $permission->setEmailAddress('[email protected]');
    $permission = $this->googleDriveClient->permissions->create( $folder->getId(),$permission,array('transferOwnership'=>true));
    $this->googleDriveClient->files->delete($folder->getId());
}

Result:

[Google_Service_Exception] {
"error": {
"errors": [
{
"domain": "global",
"reason": "internalError",
"message": "Internal Error"
}
],
"code": 500,
"message": "Internal Error" } }

2.Set permission type to anyone

public function deleteFolder(\Google_Service_Drive_DriveFile $folder)
{
    $permission = new \Google_Service_Drive_Permission();
    $permission->setRole( 'owner' );
    $permission->setType( 'anyone' );
    $permission = $this->googleDriveClient->permissions->create( $folder->getId(),$permission,array('transferOwnership'=>true));
    $this->googleDriveClient->files->delete($folder->getId());
}

Result:

[Google_Service_Exception]
{
"error": {

"errors": [                                                                
 {                                                                         
  "domain": "global",                                                      
  "reason": "insufficientFilePermissions",                                 
  "message": "The user does not have sufficient permissions for this file  ."                                                         

 }                                                                         
],                                                                         
"code": 403,                                                               
"message": "The user does not have sufficient permissions for this file."      }                                                         

}

try 3: Impersonate the creator

$this->googleClient->setAccessType('offline');
$this->googleClient->setSubject('[email protected]');//if removed everything works 

[GuzzleHttp\Exception\ClientException]
Client error: POST https://www.googleapis.com/oauth2/v4/token resulted in a 401 Unauthorized response:
{
"error": "unauthorized_client",
"error_description": "Unauthorized client or scope in request.",
"error_uri": ""
}

Am I missing something?

1
is there something in the folder? Did you remove the permissions for the other people? I would stick with your first version it looks close. Note drive permissions are a pain. - DaImTo
Folder is empty,I am just trying to add a permission to get rid of it. Pain dearly noted - ka_lin
Ideas: Do a permissions.list after your create see what it created. If there is anyone else once you have owner rights delete them. then list again to make sure it actually did it. Then delete the folder. (Like pulling teeth pain) - DaImTo
hey , did you managed to solve 2? I'm stuck on this in my app ( - aleXela
@aleXela Sorry I couldn't, you could move them to a temp directory and delete items from time to time. The project I worked on was postponed so I didn't investigate further... - ka_lin

1 Answers

1
votes

Only the owner of the folder can delete the folder. Use a service account Using OAuth 2.0 for Server to Server Applications, take the identity of the owner of the folder Perform Google Apps Domain-Wide Delegation of Authority.

Once authorized as the owner of the folder it should be possible to delete the folder.