我想使用Python获取Google云端硬盘中给定文件夹中所有文件/文件夹的列表。我正在使用的电话是:
query = parentID + " in parents"
response = service.files().list(q=query,
spaces='drive',
fields='files(id, name, parents)').execute()
根据search for files文档和migrating to v3文档,我应该正确地执行此操作。但是当我运行代码时,我收到以下错误:
<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">
我的查询有什么问题?如何正确调用?
答案 0 :(得分:7)
尝试了大约15个小时后,找到了这个解决方案:
只需更改查询变量,如下所示 假设父ID是'0B_hg54vjh34v5jh23gv5i2v35th2gv35v235kjvk'
query="'0B_hg54vjh34v5jh23gv5i2v35th2gv35v235kjvk' in parents"
现在,它应该有用。
即使你交换单引号和双引号也应该有效...你在query="0B_hg54vjh34v5jh23gv5i2v35th2gv35v235kjvk in parents"
使用的结果是什么,而我们需要的是query="'0B_hg54vjh34v5jh23gv5i2v35th2gv35v235kjvk' in parents"
这样做的替代方法是:
parentId='0B_hg54vjh34v5jh23gv5i2v35th2gv35v235kjvk'
query="'" + "' in parents"
为什么会发生这种情况: -
如果你正确看待输出错误,
它有一个http链接,如下所示:
https://www.googleapis.com/drive/v3/files?q=0B_hg54vjh34v5jh23gv5i2v35th2gv35v235kjvk+in+parents&spaces=drive&alt=json&fields=files%28id%2C+name%2C+parents%29
但是所需的一个在Id周围有单引号,看起来像这样:
https://www.googleapis.com/drive/v3/files?q='0B_hg54vjh34v5jh23gv5i2v35th2gv35v235kjvk'+in+parents&spaces=drive&alt=json&fields=files%28id%2C+name%2C+parents%29