Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving files with Google Drive API v3

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?

like image 447
MetalJacketNL Avatar asked Feb 04 '23 20:02

MetalJacketNL


1 Answers

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();
like image 56
DaImTo Avatar answered Feb 19 '23 20:02

DaImTo