在Google Drive python中搜索文件夹



问题是我想将文件上传到google drive我已经排序的文件,但问题是我需要在gdrive中获得名为Location的文件夹的folder_id。通常我会使用谷歌提供的库,但我不能用我的access_token进行身份验证,所以我需要通过REST api来完成。

我已经尝试过搜索后,但总是得到Invalid Value,如果我做

files = requests.get("https://www.googleapis.com/drive/v3/files?access_token=" + access_token + "&q=name%20%3D%20%27Location%27")

只是为了澄清如果我删除了查询,那么它就会起作用。

我非常感谢你的帮助。

我认为在您的显示脚本中,即使没有Location名称的文件和文件夹,返回的值也像"files": []而不是Invalid Value。例如,当访问令牌无效时,会出现"reason": "dailyLimitExceededUnreg""reason": "authError"这样的错误。当q的值不合法时,"location": "q"会出现Invalid Value的错误。

当我看到你的搜索查询,它是name = 'Location'。我认为这是正确的。虽然我无法复制您的情况,但是为了检索Location文件夹的文件夹ID,您可以测试以下示例脚本吗?

示例脚本1:
import requests
from urllib.parse import quote
access_token = "###" # Please set your access token.
q = "name='Location' and mimeType='application/vnd.google-apps.folder' and trashed=false"
files = requests.get(
"https://www.googleapis.com/drive/v3/files?access_token="
+ access_token
+ "&q="
+ quote(q)
)
print(files.text)
示例脚本2:
import requests
from urllib.parse import quote
access_token = "###" # Please set your access token.
q = "name='Location' and mimeType='application/vnd.google-apps.folder' and trashed=false"
files = requests.get(
"https://www.googleapis.com/drive/v3/files?&q=" + quote(q),
headers={"Authorization": "Bearer " + access_token},
)
print(files.text)

测试:

运行上述脚本时,返回如下值:

{
"kind": "drive#fileList",
"incompleteSearch": false,
"files": [
{
"kind": "drive#file",
"id": "###",
"name": "Location",
"mimeType": "application/vnd.google-apps.folder"
}
]
}

从这个结果中,您可以检索文件夹ID。

参考:

  • Files: Drive API v3列表

最新更新