google-drive-api

Google Drive API not letting me move a file to a folder


I'm trying to move a file from the root drive directory to another folder using the Google Drives API. I'm currently doing it using REST requests (no libraries), but for some reason it won't let me change the parent folder no matter what I do.

I'm calling the following rest API:

PATCH https://www.googleapis.com/drive/v3/files/<fileId>?uploadType=media&fields=id,parents,name

Here is my payload:

{
    "addParents": "<new parent ID>",
    "removeParents": "<root parent ID>"
}

I have verified that both parent IDs exist through the files.list endpoint, but it just will not let me change the parent folder. Does anyone have any idea what could be happening?


Solution

  • I thought that your showing endpoint and request body were not correct for moving the parent folder of the file. In this case, please modify it as follows.

    Endpoint:

    PATCH https://www.googleapis.com/drive/v3/files/{fileId}
    

    Query parameters:

    addParents={new parent folder ID}&fields=id,parents,name
    

    When this is converted to the curl command, it becomes as follows.

    curl --request PATCH \
      'https://www.googleapis.com/drive/v3/files/{fileId}?addParents={new parent folder ID}&fields=id%2Cparents%2Cname' \
      --header 'Authorization: Bearer {your access token}' \
      --header 'Accept: application/json' \
      --header 'Content-Type: application/json' \
      --data '{}'
    

    Reference: