gpt4 book ai didi

c# - 使用 Google Drive API v3 移动文件

转载 作者:太空狗 更新时间:2023-10-30 00:38:16 26 4
gpt4 key购买 nike

我正在尝试使用 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});
}

当我执行这段代码时,出现以下错误:

The parents field is not directly writable in update requests. Use the addParents and removeParents parameters instead.

这个错误对我来说没有意义,因为这个代码示例来自文档页面本身。我不知道他们的其他参数是什么意思。 addParents 和 removeParents 参数是什么意思? updateRequest.AddParentsupdateRequest.RemoveParents 不是正确的参数吗?

最佳答案

好的,问题来了。

var updateRequest = service.Files.Update(file, fileToMoveId);

该方法要求您发送要更新的文件主体。这通常是有道理的,因为您想要进行的任何更改都可以添加到正文中。

现在您遇到的问题是您是从 file.get 获取文件的。这是完全正常的。这就是你应该做的。问题是该文件中有一些您无法更新的字段。因此,通过发送完整文件,API 会拒绝您的更新。如果你检查 Files: update在 Request body 下,您将看到哪些 fiends 是可更新的。

问题:

现在这要么是客户端库问题,要么是 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();

关于c# - 使用 Google Drive API v3 移动文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42903908/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com