1
votes

My code below using Google Drive API v3 to transfer ownership from one user to another in the same domain is throwing this exception. I am using Service Account with domain-wide permissions for authorization piece.

string FileID = "xxxxxxxxxxxxxxxxxxx";
var getRequest1 = OwnerDrive.Files.Get(FileID);
getRequest1.Fields = "owners,name,parents,mimeType,permissions";
var afile = getRequest1.Execute();

foreach (var perm in afile.Permissions)
            {
                Console.WriteLine(perm.EmailAddress + " : " + 
                 perm.DisplayName + " : " + perm.Role + " : " + perm.Id);
            }

// I manually note down the permissionID of the current owner as 
"14016095883385444520"

//Set New owner
Permission p = new Permission();
            p.Role = "owner";
            p.Type = "user";
            p.EmailAddress = "newowner@xyz.com";

            var updateRequest = OwnerDrive.Permissions.Update(p, FileID, 
            "14016095883385444520");
            updateRequest.TransferOwnership = true;
            updateRequest.Execute();

The error\exception throw is this:

"Message[The resource body includes fields which are not directly writable.] Location[ - ] Reason[fieldNotWritable] Domain[global]".

The new drive v3 API says to use Permissions.Update and pass it permissionID which I am doing (although manually). What am I missing here ? Any help is greatly appreciated.

1
Although it looks like “Permissions.Create(p, FileID)” works for ownership transfers in V3. Not sure what would be the use case for “Permissions.Update(p, FileID, pemrissionID)” then.Nemo

1 Answers

0
votes

I think you have hit bug.

I have been testing this with the Google Apis explorer

PATCH https://www.googleapis.com/drive/v3/files/1f3eFRpZM-NAID42B9DPwUOKr6nvXuIxnfVvlQ8H1pNc/permissions/06030588225573437243?transferOwnership=true&key={YOUR_API_KEY}

{
 "emailAddress": "xxx@gmail.com"
}

Response:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "required",
    "message": "The permission role field is required.",
    "locationType": "other",
    "location": "permission.role"
   }
  ],
  "code": 400,
  "message": "The permission role field is required."
 }
}

Request attempt two

PATCH https://www.googleapis.com/drive/v3/files/1f3eFRpZM-NAID42B9DPwUOKr6nvXuIxnfVvlQ8H1pNc/permissions/06030588225573437243?transferOwnership=true&key={YOUR_API_KEY}

{
 "emailAddress": "xxx@gmail.com",
 "role": "owner"
}

Response

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "fieldNotWritable",
    "message": "The resource body includes fields which are not directly writable."
   }
  ],
  "code": 403,
  "message": "The resource body includes fields which are not directly writable."
 }
}

Bug report made here issue 73634062