Google Drive API查询所有子文件夹返回结果不全问题咨询
你当前配置的q参数语法正确,没有过滤文件夹层级的逻辑,返回结果不全通常由以下几个原因导致,对应解决方案如下:
问题排查项
- 共享云端硬盘内容未纳入搜索范围
如果你的部分文件夹存放在共享云端硬盘,或者是他人共享给你的文件夹内,Google Drive API默认仅返回你个人云端硬盘根目录下的内容,需要补充以下参数配置:
$parameters['corpora'] = 'allDrives'; $parameters['includeItemsFromAllDrives'] = true; $parameters['supportsAllDrives'] = true;
- 未处理分页返回结果
Drive API的pageSize最大可设置为1000,只要你的账号下文件夹总数超过1000,单次请求无法返回全部结果,API会同步返回nextPageToken字段,需要循环携带该字段发起多次请求,直到nextPageToken为空才能拉取到全量文件夹,参考代码如下:
$allFolders = []; $nextPageToken = null; do { $params = [ 'q' => "mimeType='application/vnd.google-apps.folder' and trashed=false", 'pageSize' => 1000, 'pageToken' => $nextPageToken, 'corpora' => 'allDrives', 'includeItemsFromAllDrives' => true, 'supportsAllDrives' => true ]; $response = $driveService->files->listFiles($params); $allFolders = array_merge($allFolders, $response->getFiles()); $nextPageToken = $response->getNextPageToken(); } while (!empty($nextPageToken));
- OAuth权限范围不足
如果申请的接口权限仅为https://www.googleapis.com/auth/drive.file这类受限范围,应用只能读取到自身创建的文件和文件夹,无法获取整个Drive的全部内容,需要将权限替换为https://www.googleapis.com/auth/drive.readonly(只读)或者https://www.googleapis.com/auth/drive(读写)。
内容的提问来源于stack exchange,提问作者StrahinjaL
相关产品推荐
相关产品推荐

