使用 Drive API 获取文件内容

Obtain file content using Drive API

我正在尝试使用 drive().files().get() 中的 v3 方法下载已上传到团队云端硬盘的文件,如 documentation 中所述。
我可以获得元数据,例如文件 ID 和权限,但不知道如何访问实际的二进制内容,例如将其写入文件。有问题的文件是纯文本文件。

这是我正在使用的代码的一部分。

    request = service.files().get(fileId=file_id, supportsTeamDrives=True)
    pprint.pprint(request.to_json())
    response = request.execute()
    pprint.pprint(response)

以及响应(来自 pprint)
要求

{
  "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
}

文件元数据

{'id': '1CxxxxxxxxxxxxHp',
 'kind': 'drive#file',
 'mimeType': 'text/plain',
 'name': 'test.txt',
 'teamDriveId': 'XXXXXXXXXXXXXXX'}

我可以访问文件元数据,但不知道如何获取文件的内容,如何写入文件。

我正在使用完全访问范围,“https://www.googleapis.com/auth/drive”。

文档说 "Gets a file's metadata or content by 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()

Google Api 文档在很多方面都非常混乱和不一致。