Ckn*_*knu 5 google-drive-api google-api-python-client
我正在尝试使用文档中drive().files().get()所述的 v3 方法下载已上传到团队云端硬盘的文件。
我可以获得元数据,如文件 ID 和权限,但不知道如何访问实际的二进制内容,例如将其写入文件。有问题的文件是纯文本文件。
这是我正在使用的代码的一部分。
request = service.files().get(fileId=file_id, supportsTeamDrives=True)
pprint.pprint(request.to_json())
response = request.execute()
pprint.pprint(response)
Run Code Online (Sandbox Code Playgroud)
和响应(来自 pprints)
请求
{
"uri": "https://www.googleapis.com/drive/v3/files/1CxxxxxxxxxxxxHp?supportsTeamDrives=true&alt=json",
"method": "GET",
"body": null,
"headers": {
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"user-agent": "google-api-python-client/1.7.8 (gzip)"
},
"methodId": "drive.files.get",
"resumable": null,
"response_callbacks": [],
"_in_error_state": false,
"body_size": 0,
"resumable_uri": null,
"resumable_progress": 0
}
Run Code Online (Sandbox Code Playgroud)
文件元数据
{
"uri": "https://www.googleapis.com/drive/v3/files/1CxxxxxxxxxxxxHp?supportsTeamDrives=true&alt=json",
"method": "GET",
"body": null,
"headers": {
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"user-agent": "google-api-python-client/1.7.8 (gzip)"
},
"methodId": "drive.files.get",
"resumable": null,
"response_callbacks": [],
"_in_error_state": false,
"body_size": 0,
"resumable_uri": null,
"resumable_progress": 0
}
Run Code Online (Sandbox Code Playgroud)
我可以访问文件元数据,但不知道如何获取文件内容以写入文件。
我正在使用完整的访问范围,“ https://www.googleapis.com/auth/drive ”。
文档说“通过 ID 获取文件的元数据或内容。”,但没有解释如何。
好的。在尝试了不同的选项后,我找到了一个混合来自不同帖子的信息的解决方案。
1 - get_media() 方法在 v3 中有效,但没有记录在任何地方(即使在 v2 文档中)。2 - io.BytesIO 不起作用,改为 FileIO。
结果代码是这样的:
request = drive_service.files().get_media(fileId=file_id)
fh = io.FileIO(filename, "wb")
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
Run Code Online (Sandbox Code Playgroud)
Google Api 文档在很多方面都非常混乱和不一致。
| 归档时间: |
|
| 查看次数: |
3528 次 |
| 最近记录: |