且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Google Drive API v3移动文件

更新时间:2023-02-14 12:33:41

确定这是问题。


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