我相信你的目标如下。
在这种情况下,为了将文件内容检索到内存中,我建议使用requests
. 为此,从creds
of中检索访问令牌get_gdrive_service()
。
为了获取文件内容,使用“Files:get”的方法,加入查询参数alt=media
。
示例脚本:
file_id = "###" # Please set the file ID you want to download.
access_token = creds.token
url = "https://www.googleapis.com/drive/v3/files/" + file_id + "?alt=media"
res = requests.get(url, headers={"Authorization": "Bearer " + access_token})
obj = json.loads(res.text)
print(obj)
- 在上面的脚本中,
creds
ofcreds.token
来自get_gdrive_service()
.
- 根据您的问题,我认为您要下载的文件是 JSON 数据。所以在上面的脚本中,下载的数据被解析为 JSON 对象。
- 在这种情况下,请导入
json
和requests
.
笔记:
- 当返回的内容是 JSON 数据时,我觉得你也可以
res.json()
用res.text
. 但是当JSONDecodeError
发生时,请检查 的值res.text
。
参考: