Why Won't Google API V3 Return Children?
Asked Answered
C

1

9

I want to use Python to get a list of all the files/folders in a given folder in Google Drive. The call I'm using is this:

query = parentID + " in parents"

response = service.files().list(q=query,
                                spaces='drive',
                                fields='files(id, name, parents)').execute()

According to the search for files documentation and the migrating to v3 documentation, I should be doing it correctly. But when I run the code, I get the following error:

<HttpError 400 when requesting https://www.googleapis.com/drive/v3/files?q=[MY_PARENT_ID]+in+parents&spaces=drive&alt=json&fields=files%28id%2C+name%2C+parents%29 returned "Invalid Value">

What is wrong with my query and how would I call this correctly?

Circumscissile answered 26/4, 2016 at 19:37 Comment(1)
Mistaken tag. Thank you for fixingCircumscissile
R
24

After trying for some 15 hours, found this solution:

just change the query variable as follows suppose parent id is '0B_hg54vjh34v5jh23gv5i2v35th2gv35v235kjvk'

query="'0B_hg54vjh34v5jh23gv5i2v35th2gv35v235kjvk' in parents"

now, it should work.

it should work even if you interchange single and double quotes..what you were using results into query="0B_hg54vjh34v5jh23gv5i2v35th2gv35v235kjvk in parents" while what we need is query="'0B_hg54vjh34v5jh23gv5i2v35th2gv35v235kjvk' in parents"

alternative way of doing this is:

parentId='0B_hg54vjh34v5jh23gv5i2v35th2gv35v235kjvk'
query="'" + "' in parents"

Why is this happening :-
If you look at your output error properly, It has a http link which looks like this:
https://www.googleapis.com/drive/v3/files?q=0B_hg54vjh34v5jh23gv5i2v35th2gv35v235kjvk+in+parents&spaces=drive&alt=json&fields=files%28id%2C+name%2C+parents%29


but the required one has single quotes around the Id and looks like this:
https://www.googleapis.com/drive/v3/files?q='0B_hg54vjh34v5jh23gv5i2v35th2gv35v235kjvk'+in+parents&spaces=drive&alt=json&fields=files%28id%2C+name%2C+parents%29

Rufena answered 20/7, 2016 at 10:51 Comment(4)
How is that different than what I was trying? E.g. If I did query = parentID + " in parents" and parentId='0B_hg54vjh34v5jh23gv5i2v35th2gv35v235kjvk', then query = parentID + " in parents" is the same as query="'0B_hg54vjh34v5jh23gv5i2v35th2gv35v235kjvk' in parents" right? Or is the difference the single quotes within the double-quoted string?Circumscissile
Thank you! That makes a lot of senseCircumscissile
This is one of the most confusing ways I've ever come across to list the contents of a folder and the documentation is not clear on this point at all. Thank you!Horned
wow, i never would have figured this out. this is the most cryptic api in the world.Behnken

© 2022 - 2024 — McMap. All rights reserved.