ka_lin
ka_lin

Reputation: 9432

How to delete folder drive api

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?

Upvotes: 1

Views: 4634

Answers (1)

Jasper Duizendstra
Jasper Duizendstra

Reputation: 2617

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.

Upvotes: 1

Related Questions