我正在尝试使用Google Drive API v3将文件从一个文件夹移动到另一个文件夹。我找到了文档如何here。我使用了文档页面中的.NET示例代码,并创建了一个如下所示的方法:
public ActionResult MoveFile(string fileToMove, string destination)
{
DriveService service = new DriveService(new BaseClientService.Initializer
{
HttpClientInitializer = <USER CREDENTIAL>,
ApplicationName = "APPNAME"
});
var searchFiles = service.Files.List();
searchFiles.Corpus = FilesResource.ListRequest.CorpusEnum.User;
searchFiles.Q = "name = '" + fileToMove + "'";
searchFiles.Fields = "files(*)";
string fileToMoveId = searchFiles.Execute().Files[0].Id;
searchFiles.Q = "name = '" + destination + "'";
string destinationId = searchFiles.Execute().Files[0].Id;
//Code used from documentation
// Retrieve the existing parents to remove
var getRequest = service.Files.Get(fileToMoveId);
getRequest.Fields = "parents";
var file = getRequest.Execute();
var previousParents = String.Join(",", file.Parents);
// Move the file to the new folder
var updateRequest = service.Files.Update(file, fileToMoveId);
updateRequest.Fields = "id, parents";
updateRequest.AddParents = destinationId;
updateRequest.RemoveParents = previousParents;
file = updateRequest.Execute();
return RedirectToAction("Files", new {folderId = destinationId});
}
当我执行此代码时,出现以下错误:
父节点字段在更新请求中不可直接写入。使用 改为添加父母和removeParents参数。
这个错误对我来说没有意义,因为这个代码示例来自文档页面本身。我无法弄清楚他们的意思是什么。 addParents和removeParents参数是什么意思? updateRequest.AddParents
和updateRequest.RemoveParents
不是正确的参数吗?
答案 0 :(得分:7)
好的,这就是问题。
var updateRequest = service.Files.Update(file,fileToMoveId);
该方法要求您发送要更新的文件正文。这通常是有意义的,因为您想要的任何更改都可以添加到正文中。
现在你遇到的问题是你从file.get获得了你的文件。这完全正常。这就是你应该这样做的方式。问题是该文件中有一些字段无法更新。因此,通过发送完整文件,API拒绝您的更新。如果您在“请求正文”下检查Files: update,您将看到哪些恶魔可以更新。
<强>问题强>:
现在这是客户端库或API的一个问题,我要跟踪Google上的一些人,看看情况如何。
<强>修正强>:
我做了一些测试并发送了一个空文件对象,因为正常工作正常。文件被移动。
var updateRequest = service.Files.Update(new Google.Apis.Drive.v3.Data.File(), fileToMove.Id);
updateRequest.AddParents = directoryToMove.Id;
updateRequest.RemoveParents = fileToMove.Parents[0];
var movedFile = updateRequest.Execute();
答案 1 :(得分:-1)
在您自己的驱动器中工作时,此方法效果很好,但在文件(文件夹)只能具有1个父级的团队驱动器中,此方法效果不佳。我没有团队合作的解决方案