使用Google Drive API v3移动文件 [英] Moving files with Google Drive API v3

查看:758
本文介绍了使用Google Drive API v3移动文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Google Drive API v3将文件从一个文件夹移动到另一个文件夹。我找到了有关如何此处的文档。我使用了文档页面中的.NET示例代码,并创建了如下所示的方法:

  public ActionResult MoveFile(string fileToMove ,
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 =文件(*);

string fileToMoveId = searchFiles.Execute()。Files [0] .Id;

searchFiles.Q =name ='+ destination +';
string destinationId = searchFiles.Execute()。Files [0] .Id;

//从文档
中使用的代码//检索现有的父项以删除
var getRequest = service.Files.Get(fileToMoveId);
getRequest.Fields =父母;
var file = getRequest.Execute();
var previousParents = String.Join(,,file.Parents);

//将文件移动到新文件夹
var updateRequest = service.Files.Update(file,fileToMoveId);
updateRequest.Fields =id,parents;
updateRequest.AddParents = destinationId;
updateRequest.RemoveParents = previousParents;
file = updateRequest.Execute();

返回RedirectToAction(文件,新的{folderId = destinationId});
}

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


父项字段在更新请求中不能直接写入。改为使用
addParents和removeParents参数。

该错误对我来说并不合理,因为此代码示例从文档页面本身。我无法弄清楚他们的其他参数。 addParents和removeParents参数意味着什么?是 updateRequest.AddParents updateRequest.RemoveParents 不正确的参数?

解决方案

确定这是问题。


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

该方法要求您发送要更新的文件的主体。这通常是有意义的,因为您希望使您可以添加到正文中的任何更改。

现在您遇到的问题是您从file.get中获取了文件。这是完全正常的。这就是你应该如何做的。问题是该文件中有一些字段不能更新。因此,通过发送完整文件API将拒绝您的更新。如果您在请求正文下检查文件:更新,您将看到哪些恶魔是可更新的。



问题

现在,客户端库或API,我将不得不在Google中追踪一些人,看看是哪种情况。



修复



我做了一些测试并发送一个空文件对象,因为身体工作得很好。
$ b

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


Im trying to move a file from one folder to another using the Google Drive API v3. I found documentation how to this here. I used the .NET sample code from the documentation page and created a method that looks like this:

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

When I execute this code I get the following error:

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

The error doesn't really makes sense to me because this code sample came from the documentation page itself. I can't figure out what other paramters they mean. What addParents and removeParents parameters do they mean? Are updateRequest.AddParents and updateRequest.RemoveParents not the right parameters?

解决方案

Ok here is the problem.

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

The method is requiring that you send a body of a file to be updated. This normally makes sense as any changes you want to make you can add to the body.

Now the problem you are having is that you got your file from a file.get. Which is totally normal. This is how you should be doing it. THe problem is there are some fields in that file that you cant update. So by sending the full file the API is rejecting your update. If you check Files: update under Request body you will see which fiends are updateable.

Issue:

Now this is either a problem with the client library or the API I am going to have to track down a few people at Google to see which is the case.

Fix:

I did some testing and sending an empty file object as the body works just fine. The file is moved.

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

这篇关于使用Google Drive API v3移动文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆