重命名google驱动器文件C#
我正在尝试使用google drive v3 API重命名google drive文件。 这是我的密码:重命名google驱动器文件C#,c#,google-drive-api,rename,C#,Google Drive Api,Rename,我正在尝试使用google drive v3 API重命名google drive文件。 这是我的密码: public async Task<File> RenameFile(String fileId, String newFilename) { try { DriveService service = await GetDriveService();
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;
}
公共异步任务重命名文件(字符串文件ID,
字符串(新文件名)
{
尝试
{
DriveService=等待GetDriveService();
//首先从API中检索文件。
Google.api.Drive.v3.Data.File File=service.Files.Get(fileId.Execute();
//文件的新元数据。
//file.Name=newFilename;
file.OriginalFilename=newFilename;
File newFile=新文件();
newFile=文件;
//将请求发送到API。
fileresource.UpdateRequest请求=service.Files.Update(newFile,fileId);
request.Fields=“id,name,thumbnailLink”;
File uploadedFile=request.Execute();
返回上传的文件;
}
捕获(例外e)
{
Console.WriteLine(“发生错误:+e.Message”);
返回null;
}
这将抛出一个错误:
出现错误:Google.api.Requests.RequestError
资源主体包含不可直接写入的字段。[403]
错误[
消息[资源正文包括不可直接写入的字段。]位置[-]原因[fieldNotWritable]域[全局]
]
请帮助我,我已经尝试了很多方法,但都不起作用
我的驱动器和文件仅与单个用户共享,我使用控制台自托管服务与驱动器交互,因此最终是单个用户通过API使用驱动器。您的driveService.files.Get(fileId).Execute()请求返回一个具有无法覆盖的file.Id的对象。因此,将其设置为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();
}
您只能重命名您拥有的文件或文件夹。否则,您将收到消息错误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] ]
这里的解决方案是:您可以复制或使用所有者用户的凭据
希望有此帮助。它正在使用v2,但在v3中得到相同的错误。参考:的可能重复