Node JS Google Drive api 方法列表 returns 已删除文件

Node JS Google Drive api method list returns deleted files

我正在使用以下函数从 Google 驱动器获取文件列表:

const { google } = require('googleapis');
const SCOPES = ['https://www.googleapis.com/auth/drive'];
const KETFILEPATH = "key.json"
const auth = new google.auth.GoogleAuth({
    keyFile: KETFILEPATH,
    scopes: SCOPES
})

let files_list = async (auth, nextPageToken = null, res = []) => {
    const driveService = google.drive({ version: 'v3', auth })
    const list = await driveService.files.list({
        pageToken: nextPageToken,
        fields: 'nextPageToken, files(id, name)',
      });
      const files = list.data.files;
      if (files.length === 0) {
        console.log('No files found.');
      } else {
        for (const file of files) {
            res.push(file)
        }
        if (list.data.nextPageToken) {
            return await files_list(auth, list.data.nextPageToken, res)
        } else {
            return res
        }
      }
}

但是这个功能 returns 我有很多文件,我已经从 https://drive.google.com/ 网站上删除了。我也有清空垃圾桶。更重要的是,当我尝试使用此功能删除此文件时:

const { google } = require('googleapis');
const SCOPES = ['https://www.googleapis.com/auth/drive'];
const KETFILEPATH = "key.json"
const auth = new google.auth.GoogleAuth({
    keyFile: KETFILEPATH,
    scopes: SCOPES
})

let deleteFile = async (auth, file_id) => {
    const driveService = google.drive({ version: 'v3', auth })
    let res = await driveService.files.delete({ 'fileId': file_id })
    if (res.status != 200) {
        console.log(res)
        console.log("Error deleting file")
    }
}

我得到这个输出:

{
  config: {
    url: 'https://www.googleapis.com/drive/v3/files/1Iddlq9Mri76rLhDi3TGCTYM-HEhVtEKL',
    method: 'DELETE',
    userAgentDirectives: [ [Object] ],
    paramsSerializer: [Function (anonymous)],
    headers: {
      'x-goog-api-client': 'gdcl/5.0.2 gl-node/16.3.0 auth/7.2.0',
      'Accept-Encoding': 'gzip',
      'User-Agent': 'google-api-nodejs-client/5.0.2 (gzip)',
      Authorization: '...',
      Accept: 'application/json'
    },
    params: {},
    validateStatus: [Function (anonymous)],
    retry: true,
    responseType: 'json'
  },
  data: '',
  headers: {
    'alt-svc': 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"',
    'cache-control': 'no-cache, no-store, max-age=0, must-revalidate',
    connection: 'close',
    date: 'Wed, 07 Jul 2021 14:35:10 GMT',
    expires: 'Mon, 01 Jan 1990 00:00:00 GMT',
    pragma: 'no-cache',
    server: 'GSE',
    vary: 'Origin, X-Origin'
  },
  status: 204,
  statusText: 'No Content',
  request: {
    responseURL: 'https://www.googleapis.com/drive/v3/files/1Iddlq9Mri76rLhDi3TGCTYM-HEhVtEKL'
  }
}

我使用服务帐户进行身份验证。我已经和这个账号共享了一个文件夹,他需要处理。

我该如何处理?如何获取存储在 Google 驱动器中的真实文件列表?

您已与服务帐户共享一个文件夹。但是您正在请求服务帐户有权访问的所有文件。

 const driveService = google.drive({ version: 'v3', auth })
    const list = await driveService.files.list({
        pageToken: nextPageToken,
        fields: 'nextPageToken, files(id, name)',
      });

您尚未添加 parents,因此您不是在请求所有文件的单个文件夹中查找。您是否认为您看到的文件是服务帐户 google 驱动器帐户上的文件?

const driveService = google.drive({ version: 'v3', auth })
    const list = await driveService.files.list({
        pageToken: nextPageToken,
        q: `'${FolderId}' in parents`
        fields: 'nextPageToken, files(id, name)',
      });

您必须使用以下内容

 let result = await drive.files.list({
    q: "mimeType='application/vnd.google-apps.folder' and trashed=false",
    fields: 'nextPageToken, files(id, name)',
    spaces: 'drive',
});

在此thrashed=false中将不会检查Thrash中的文件。祝一切顺利

你需要使用 includeRemoved: false

const driveService = google.drive({ version: 'v3', auth })
const list = await driveService.files.list({
    pageToken: nextPageToken,
    includeRemoved: false,
    q: `'${FolderId}' in parents`
    fields: 'nextPageToken, files(id, name)',
  });