GeoDirk
GeoDirk

Reputation: 17

C# Delete File from Google Shared Drive API v.3

I'm successfully able to upload a file to a Google Drive Shared Drive (not the "My Drive" folder which is different) using a service account. But when I try to delete the file, I'm getting a The user does not have sufficient permissions for this file. [403]

Note that in my code I have the request.SupportsAllDrives = true; flag set to allow it access to shared drives.

I've done a check on the service account's permission ID and also the file's permission ID and unsurprisingly, they are the same since the service account is the one who put the file up there to begin with.

Any ideas of what is going wrong?

/// <summary>  
/// Permanently delete a file, skipping the trash.  
/// </summary>  
/// <param name="service">Drive API service instance.</param>  
/// <param name="fileId">ID of the file to delete.</param> 
public async Task<string> DeleteDriveFile(DriveService service, string fileId)
{
    // get the user's permission id from Drive
    var getRequest = service.About.Get();
    getRequest.Fields = "*";
    var getResponse = await getRequest.ExecuteAsync().ConfigureAwait(true);

    string userPermissionId = getResponse.User.PermissionId;
    Debug.Print($"UserPermissionID: {userPermissionId}");

    var responseFile = service.Permissions.Get(fileId, userPermissionId);
    Debug.Print($"FilePermissionID: {responseFile.PermissionId}");

    string response = "";
    try
    {
        var request = service.Files.Delete(fileId);
        request.SupportsAllDrives = true;
        response = await request.ExecuteAsync().ConfigureAwait(false);
    }
    catch (Exception e)
    {
        Debug.WriteLine("Delete File Error: " + e.Message);
    }

    return response;
}

What gets written to the output console:

UserPermissionID: 0243088xxxxxx3009857
FilePermissionID: 0243088xxxxxx3009857
The thread 0x1398 has exited with code 0 (0x0).
Exception thrown: 'Google.GoogleApiException' in System.Private.CoreLib.dll
Delete File Error: Google.Apis.Requests.RequestError
The user does not have sufficient permissions for this file. [403]
Errors [
    Message[The user does not have sufficient permissions for this file.] Location[ - ] Reason[insufficientFilePermissions] Domain[global]
]

Upvotes: 0

Views: 1563

Answers (1)

Amir Hajiha
Amir Hajiha

Reputation: 935

The currently authenticated user must own the file or be an organizer on the parent for shared drive files.

So in summary, you cannot delete a file if it has not been created by you (i.e. the user authenticated to the API)

Upvotes: 0

Related Questions