0

I am trying to rename a google drive file using google drive v3 Apis. here is my code:

public async Task<File> RenameFile(String fileId,
             String newFilename)
        {
            try
            {
                DriveService service = await GetDriveService();
                // First retrieve the file from the API.
                Google.Apis.Drive.v3.Data.File file = service.Files.Get(fileId).Execute();

                // File's new metadata.
                // file.Name = newFilename;
                file.OriginalFilename = newFilename;
                File newFile = new File();
                newFile = file;
                // Send the request to the API.
                FilesResource.UpdateRequest request = service.Files.Update(newFile, fileId);
                request.Fields = "id, name, thumbnailLink";
                File uploadedFile = request.Execute();

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

this is throwing an error:

An error occurred: 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] ]

Please help me with this, I have tried many ways, but its not working.

My drive and files are shared with the single user only, and I am using a console self hosted service to interact with drive, so finally its a single user who is using drive through API.

Pankaj Nema
  • 165
  • 2
  • 13
  • It is working with v2, but getting the same error in v3. ref: https://developers.google.com/drive/api/v2/reference/files/patch – Pankaj Nema Sep 15 '19 at 18:44
  • Possible duplicate of [Unable to update Google Drive files using Drive API v3 -- The resource body includes fields which are not directly writable](https://stackoverflow.com/questions/39212019/unable-to-update-google-drive-files-using-drive-api-v3-the-resource-body-incl) – Diogo Rocha Sep 15 '19 at 20:10

2 Answers2

2

Your driveService.Files.Get(fileId).Execute() request returns an object with a file.Id which cannot be overwritten. So, set it to null.

        private static void OverwriteDriveFileNames(DriveService driveService)
    {
        string fileId = "myId";
        Google.Apis.Drive.v3.Data.File file = driveService.Files.Get(fileId).Execute();
        file.Id = null;
        FilesResource.UpdateRequest request = driveService.Files.Update(file, fileId);
        request.Execute();
    }
0

You only rename the files or folders you own. Otherwise you will get message error 403:

An error occurred: 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] ]

The solution here: You can make a copy or use Credential of owner's user.

Hope this help.

Datusa
  • 101
  • 1
  • 3