简体   繁体   English

重命名谷歌驱动器文件 C#

[英]Renaming a google drive file C#

I am trying to rename a google drive file using google drive v3 Apis.我正在尝试使用 google drive v3 Apis 重命名 google drive 文件。 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.发生错误:Google.Apis.Requests.RequestError 资源正文包含不可直接写入的字段。 [403] Errors [ Message[The resource body includes fields which are not directly writable.] Location[ - ] Reason[fieldNotWritable] Domain[global] ] [403] 错误 [消息[资源主体包括不可直接写的字段。]位置[-]原因[fieldNotWritable]域[全局]]

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.我的驱动器和文件仅与单个用户共享,并且我使用控制台自托管服务与驱动器交互,因此最终它是通过 API 使用驱动器的单个用户。

Your driveService.Files.Get(fileId).Execute() request returns an object with a file.Id which cannot be overwritten.您的 driveService.Files.Get(fileId).Execute() 请求返回一个带有无法覆盖的 file.Id 的对象。 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();
    }

You only rename the files or folders you own.您只需重命名您拥有的文件或文件夹。 Otherwise you will get message error 403:否则,您将收到消息错误 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.希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM