Google API,drive.files.list和仅返回子文件

And*_*e M 4 node.js google-drive-api google-api-nodejs-client

我在NodeJS中使用' googleapis '库,我正在尝试返回当前指定文件夹中的文件和文件夹列表,但我发现drive.files.list返回用户已被授予读取权限的所有文件.

我的目标是能够下载给定文件夹下的文件夹结构,然后NodeJS应用程序可以以有意义的方式使用它.

我使用的代码如下:

const fs = require('fs');
const google = require('googleapis');
const OAuth2 = google.auth.OAuth2;
const key = require('./key.json');

const jwtClient = new google.auth.JWT(
  key.client_email,
  null,
  key.private_key,
  ['https://www.googleapis.com/auth/drive'],
  null
);

const drive = google.drive({
  version: 'v3',
  auth: jwtClient
});

jwtClient.authorize(function (err, tokens) {
  if (err) {
    console.log(err);
    return;
  }

  // Make an authorized request to list Drive files.
  drive.files.list({
     includeRemoved: false,
     spaces: 'drive',
     fileId: 'the-file-id-of-the-folder'
  }, function (err, resp) {
    if (!err) {
        var i;
        var files = resp.files;
        for (i=0; i<files.length; i++) {
            if (files[i].mimeType !== 'application/vnd.google-apps.folder') {
                console.log('file: ' + files[i].name);
            } else {
                console.log('directory: ' + files[i].name);
            }
            console.log(files[i]);
        }
    } else {
        console.log('error: ', err);
    }
  });
});
Run Code Online (Sandbox Code Playgroud)

注意我确实尝试了drive.files.get端点,没有alt: 'media'参数,但它似乎没有返回drive.files.list调用的任何更多元数据.

And*_*e M 10

进一步探索导致" 搜索参数"页面,其在API的v3中指示还存在"字段"和"q"参数,使得列表操作变为(ES6代码):

var fileId = '0B0gSXXXXXXXXXXXXXXXXb0hpaWM'
drive.files.list({
    includeRemoved: false,
    spaces: 'drive',
    fileId: fileId,
    fields: 'nextPageToken, files(id, name, parents, mimeType, modifiedTime)',
    q: `'${fileId}' in parents`
}, function (err, response) {
   // TODO handle response
});
Run Code Online (Sandbox Code Playgroud)

注意,对于其他元数据,请参阅:

https://developers.google.com/drive/v3/reference/files#resource