Reputation: 9432
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 :
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',
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 a401 Unauthorized
response:
{
"error": "unauthorized_client",
"error_description": "Unauthorized client or scope in request.",
"error_uri": ""
}
Am I missing something?
Upvotes: 1
Views: 4634
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