3

Same problem as Transfer ownership of file uploaded using Google drive API - seems like no solution there. I've added my own question a) for different language, and b) simpler code, with two methods tried. I've tried one of the answers from that, and don't think the other applies.

I need to transfer ownership of a Google Drive file or folder from me to someone else. If the file was created in Google Drive (web) then it works fine. If the file was created by the API (eg through Google Drive Windows app) then it fails with Insufficient permissions for this file [403]

I'm not using Google Apps for Work, so can't impersonate or use the Admin tool.

I've tried:

    public bool ChangeItemOwnership(DriveService service, string fileId, string myPermissionId, string toPermissionId)
    {
        try
        {
            Permission permission = service.Permissions.Get(fileId, myPermissionId).Execute();
            if (permission.Role == "owner")
            {
                permission = service.Permissions.Get(fileId, toPermissionId).Execute();
                permission.Role = "owner";
                PermissionsResource.UpdateRequest updatePermission = service.Permissions.Update(permission, fileId, toPermissionId);
                updatePermission.TransferOwnership = true;
                permission = updatePermission.Execute();
            }
        }
        catch
        {
            return false;
        }
        return true;
    }

and

    public bool ChangeItemOwnership(DriveService service, string fileId, string myPermissionId, string toPermissionId)
    {
        try
        {
            Permission permission = service.Permissions.Get(fileId, myPermissionId).Execute();
            if (permission.Role == "owner")
            {
                Permission patchedPermission = new Permission();
                patchedPermission.Role = "owner";
                PermissionsResource.PatchRequest patchPermission = service.Permissions.Patch(patchedPermission, fileId, toPermissionId);
                patchPermission.TransferOwnership = true;    
                patchPermission.Execute();
            }
        }
        catch
        {
            return false;
        }
        return true;
    }

FYI, I authorise with:

    static string[] Scopes = { DriveService.Scope.Drive };

            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(_resourceStream).Secrets,
                Scopes,
                "user",
                CancellationToken.None,
                new FileDataStore(credPath, true)).Result;

I wondered whether the problem might be the "user" above but I haven't seen any other answer to what that should read - I tried putting my email address but same result. Also considered different scopes but can't see any that might apply.

It even fails on Google's API pages:

https://developers.google.com/drive/v2/reference/permissions/update

fileId = a file's Id - try one created through Google Drive Windows app
permissionId = from https://developers.google.com/drive/v2/reference/permissions/getIdForEmail
transferOwnership = true
Request body: role = owner

Really stuck on this. Can anyone help?


EDIT: As requested, the permissions relating to me, my user, on the file. I've hidden some things which might be better hidden (not sure). The only differences are the ETag and the SelfLink.

I get fromPermissionId by:

About about = service.About.Get().Execute();
fromPermissionId = about.PermissionId;

Below is permission from after the line:

Permission permission = service.Permissions.Get(fileId, fromPermissionId).Execute();

FILE FOR WHICH OWNERSHIP CANNOT BE HANDED OVER:

permission
{Google.Apis.Drive.v2.Data.Permission}
AdditionalRoles: null
AuthKey: null
Domain: "gmail.com"
EmailAddress: "***********@gmail.com"
ETag: "\"fbeGFVkC******djp1CYyuwDABw/aPlNOCkOt******X9klsSp45w\""
Id: "1726185******1763882"
Kind: "drive#permission"
Name: "James Carlyle-Clarke"
PhotoLink: null
Role: "owner"
SelfLink: "https://www.googleapis.com/drive/v2/files/0B******DI_IeTk9z******ZTRkk/permissions/1726185******1763882"
Type: "user"
Value: null
WithLink: null

FILE FOR WHICH OWNERSHIP CAN BE HANDED OVER:

permission
{Google.Apis.Drive.v2.Data.Permission}
AdditionalRoles: null
AuthKey: null
Domain: "gmail.com"
EmailAddress: "***********@gmail.com"
ETag: "\"fbeGFVkC******djp1CYyuwDABw/cT4fcr2Tka******EpimOAHa4hw\""
Id: "1726185******1763882"
Kind: "drive#permission"
Name: "James Carlyle-Clarke"
PhotoLink: null
Role: "owner"
SelfLink: "https://www.googleapis.com/drive/v2/files/1Uyg0o******hZKvIRS-ghAi0GeJ******7029Eofr1o/permissions/1726185******1763882"
Type: "user"
Value: null
WithLink: null

SECOND UPDATE: Found a post suggesting using Insert() - Google Drive API ownership of files uploaded from service account

Tried it. It failed, with error Bad Request. User message: "You can't change the owner of this item yet. (We're working on it.)" [400] Interestingly the old methods above still give the same 403 error. Still looking for a solution, but I'm hoping someone from Google has been lurking and is now working on fixing it.


THIRD UPDATE: Tried PropertyList properties = service.Properties.List(fileId).Execute();

There are no properties at all on either of the files (the one I can change ownership of, and the one I can't change ownership of).


If you need more then let me know.

Community
  • 1
  • 1
James Carlyle-Clarke
  • 830
  • 1
  • 12
  • 19
  • show us the file owners in both cases. one case (api) might be using a service account? – Zig Mandel Nov 13 '15 at 14:41
  • @ZigMandel, think this is what you were asking for? Let me know if not. – James Carlyle-Clarke Nov 14 '15 at 16:46
  • so in both cases its the same file owner. odd. – Zig Mandel Nov 14 '15 at 16:48
  • Oh yes. It's driving me crazy. I agree there must be some kind of hidden value or attached tag or whatever. I even wondered if somehow Google Drive (Windows) was 'locking' the file since it was managing it. But that would be crazy! And besides, that other link I gave said that it's ANY google API created file.... – James Carlyle-Clarke Nov 14 '15 at 16:51
  • I would be interested if anyone could try to replicate this - with the code given above should be pretty easy to do, and then just test it with a file created in the web version of GD versus one from the windows version. I could provide more code (eg creation of the DriveService, etc.) if needed. – James Carlyle-Clarke Nov 14 '15 at 17:54

0 Answers0