1
votes

I upload the file with service acconut, then I want transfer the owner permission to my own account, But it always throw an exception which is "The resource body includes fields which are not directly writable."

I read offcial document say the field "role" is writable.
https://developers.google.com/drive/api/v2/reference/permissions/update

Where do I need to Correction?

My api version is Google Apis Drive v3

Here is sample code I copied at google developers

 try
        {
            // First retrieve the permission from the API.
            Permission permission = service.Permissions.Get(fileId, permissionId).Execute();
            permission.Role = newRole;
            return service.Permissions.Update(permission, fileId, permissionId).Execute();
        }
        catch (Exception e)
        {
            Console.WriteLine("An error occurred: " + e.Message);
        }

        return null;

After Execute(), I got this error message.

Google.Apis.Requests.RequestError The resource body includes fields which are not directly writable. [403] Errors [Message[The resource body includes fields which are not directly writable.] Location[ - ] Reason[fieldNotWritable] Domain[global]]


(Update)

I changed my code with

public Permission ModifyPermission(DriveService service, string fileId, string permissionId, string newRole)
    {
        try
        {
            if (String.IsNullOrEmpty(newRole)) return null;

            var permission = new Permission
            {
                Role = newRole
            };

            var request = service.Permissions.Update(permission, fileId, permissionId);

            if(newRole == "owner") request.TransferOwnership = true;

            return request.Execute();
        }
        catch (Exception e)
        {
            Console.WriteLine("An error occurred: " + e.Message);
        }

        return null;
    }

Then it throw another exception.

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] ]


The File was upload by service account credential in JSON. And I update permission is as same credential as upload.

What am I missing?

1
insert new permissions dont update it.DaImTo

1 Answers

0
votes

The permission object that you are providing, has more properties filled than accepted by the drive api.

For example, the properties Kind and Type are filled when a permission is retrieved by using the get method.

Only the following properties of a permission are allowed when using the update method: expirationTime, role.

See: Permissions: update, section Request body.

You could try this:

try
{
    var permission = new Permission
    {
        Role = newRole
    };
    return service.Permissions.Update(permission, fileId, permissionId).Execute();
}
catch (Exception e)
{
    Console.WriteLine("An error occurred: " + e.Message);
}

return null;