【问题标题】:Moving files with Google Drive API v3使用 Google Drive API v3 移动文件
【发布时间】:2017-03-20 12:42:23
【问题描述】:

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

当我执行此代码时,我收到以下错误:

parents 字段在更新请求中不可直接写入。使用 addParents 和 removeParents 参数。

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

【问题讨论】:

  • 对于尝试更新文件时遇到此错误的任何人。您需要从元数据中删除“parents”字段并添加具有相同 parentId 的 removeParents 和 addParents 字段。

标签: c# asp.net-mvc google-api google-drive-api google-api-dotnet-client


【解决方案1】:

好的,问题来了。

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();

【讨论】:

  • @DalmTo 我删除了这一行,所以更新请求现在看起来像这样:var updateRequest = service.Files.Update(file, fileToMoveId); updateRequest.AddParents = destinationId; updateRequest.RemoveParents = previousParents; file = updateRequest.Execute(); 不幸的是,发生了同样的错误。
  • 检查我的更新我认为你发现了一个错误,但我不确定它在哪里我已经向 Google 发送了一些消息。
  • 您使用的样本现已修复。
【解决方案2】:

此方法适用于您自己的驱动器,但不适用于文件(文件夹)严格只能有 1 个父级的团队驱动器。我没有团队驱动中的解决方案

【讨论】:

  • 哪种方法?你指的是什么?这看起来不像是对原始问题的回答。
猜你喜欢
  • 2021-11-09
  • 1970-01-01
  • 1970-01-01
  • 2019-11-08
  • 1970-01-01
  • 1970-01-01
  • 2020-10-03
  • 1970-01-01
  • 2021-07-15
相关资源
最近更新 更多