I’m having trouble changing ownership of Google Drive files using the API. When I try to transfer ownership of files that were uploaded through the Google Drive desktop app, I get a 403 Insufficient permissions error. However, files created directly in the web interface work perfectly fine.
I’m not using Google Workspace so I can’t use admin features or impersonation. Here’s what I’ve tried so far:
Method 1 - Using Update:
public bool TransferFileOwner(DriveService driveService, string documentId, string currentOwnerId, string newOwnerId)
{
try
{
Permission currentPerm = driveService.Permissions.Get(documentId, currentOwnerId).Execute();
if (currentPerm.Role == "owner")
{
Permission targetPerm = driveService.Permissions.Get(documentId, newOwnerId).Execute();
targetPerm.Role = "owner";
PermissionsResource.UpdateRequest updateReq = driveService.Permissions.Update(targetPerm, documentId, newOwnerId);
updateReq.TransferOwnership = true;
targetPerm = updateReq.Execute();
}
}
catch
{
return false;
}
return true;
}
Method 2 - Using Patch:
public bool TransferFileOwner(DriveService driveService, string documentId, string currentOwnerId, string newOwnerId)
{
try
{
Permission currentPerm = driveService.Permissions.Get(documentId, currentOwnerId).Execute();
if (currentPerm.Role == "owner")
{
Permission newPermission = new Permission();
newPermission.Role = "owner";
PermissionsResource.PatchRequest patchReq = driveService.Permissions.Patch(newPermission, documentId, newOwnerId);
patchReq.TransferOwnership = true;
patchReq.Execute();
}
}
catch
{
return false;
}
return true;
}
My authorization setup:
static string[] ApiScopes = { DriveService.Scope.Drive };
credentials = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(_stream).Secrets,
ApiScopes,
"user",
CancellationToken.None,
new FileDataStore(credentialsPath, true)).Result;
Both methods fail with the same permission error. I’ve also tried using Insert() but that gives me a 400 Bad Request with the message about not being able to change ownership yet.
Has anyone found a workaround for this issue? It seems to be specifically related to files uploaded via the desktop client versus web-created files.