繁体   English   中英

Google Drive API v3:service.files().list 不返回所有文件夹

[英]Google Drive API v3: service.files().list not returning all folders

我正在根据我们的 Google Team Drive 文件的内容实现一个基于树的查看器; 为此,我遵循此处给出的建议: 如何在 Google Drive 中搜索子文件夹和子子文件夹? ,即执行 2 个 API 调用 - 一个获取所有文件夹,另一个获取作为这些文件夹的直接子级的所有文件。

但是,我注意到获取所有文件夹的调用并没有返回所有文件夹:


res = service.files().list(
corpora='teamDrive',
pageSize=1000,
supportsTeamDrives=True,
includeTeamDriveItems=True,
teamDriveId=TEAM_DRIVE_ID,
fields='files(id,parents,name,mimeType)',
q="mimeType='application/vnd.google-apps.folder' and trashed=false"
).execute()

len(res['files'])
# 460

在这里,文件不应该超过pageSize ,但我肯定会从这个结果中遗漏大量文件夹,例如一个特定的 id 为specified_id的文件夹:


len([x for x in res['files'] if x['id'] == specified_id])
# 0

我不认为这是权限问题,因为我可以正常获取此文件:


specific_file = service.files().get(
    fileId=specified_id,
    supportsTeamDrives=True,
).execute()

specific_file

{'id': '...',
 'kind': 'drive#file',
 'mimeType': 'application/vnd.google-apps.folder',
 'name': '...',
 'teamDriveId': '...'}

为了实现在一个 API 请求中获取谷歌(团队)驱动器中的所有文件夹的目标,任何其他尝试尝试的其他提示将不胜感激。

您的代码中存在错误。 您的代码假定,如果您有(比如说)500 个文件夹,那么通过提供pageSize=1000 ,所有 500 个文件夹都将包含在单个 list.execute 中。 这不是 Drive 的工作方式。

pageSize 只是一个最大值,因此最多包含 1000 个文件夹。 API 不保证最多包含 1000 个文件夹。 您需要迭代list.execute在您的请求中包含pageToken ,直到nextPageToken==null

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM