Skip to content
Advertisement

Return all files within a folder using Google Drive Java API

I’m able to retrieve all the files inside of my root folder using this code:

FileList result = driveService.files().list()
        .setPageSize(100)
        .setFields("nextPageToken, files(id, name)")
        .execute();

However I would like to return only the files inside the “test” folder under root, but this fails:

FileList result = driveService.files().list().setQ("test in parents")
            .setPageSize(100)
            .setFields("nextPageToken, files(id, name)")
            .execute();

with this error:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
GET https://www.googleapis.com/drive/v3/files?fields=nextPageToken,%20files(id,%20name)&pageSize=100&q=test%20in%20parents
{
  "code" : 400,
  "errors" : [ {
    "domain" : "global",
    "location" : "q",
    "locationType" : "parameter",
    "message" : "Invalid Value",
    "reason" : "invalid"
  } ],
  "message" : "Invalid Value"
}

Any idea what I’m doing wrong?

Advertisement

Answer

You missed the quotes around test.

And test cannot be the folder name, but the folder id.

‘FOLDER_ID’ in parents

Reference:

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement