我有一个经过身份验证的Google客户端,我想列出其他人的公共文件夹中的文件(我认为共享的文件夹或不共享的文件夹都是不相关的,因为它是公共的)

这是我在NodeJS中的代码

  const drive      = google.drive({ version: 'v3', auth });
  let fconf        = {};
  fconf.maxResults = 10;
  fconf.orderBy    = "createdTime";
  fconf.driveId    = "xyz-badfd134343example";
  fconf.includeItemsFromAllDrives = true;
  fconf.q          = "application/vnd.google-apps.spreadsheet";
  fconf.supportsTeamDrives = true;
  fconf.corpa              = "drive";
  fconf.supportsAllDrives  = true;

  drive.files.list(fconf, function(e,d)
  {
    console.log("e,d",e,d);
  });
 }


注意:文档中没有'folderId'选项:https://developers.google.com/drive/api/v3/reference/files/list-仅是driveId选项

尽管我将corpa设置为“驱动器”和驱动器ID,但出现以下错误

{ Error: The driveId parameter must be specified if and only if corpora is set to drive.
    at Gaxios.<anonymous> (/home/ubuntu/c/node_modules/gaxios/build/src/gaxios.js:73:27)
    at Generator.next (<anonymous>)
    at fulfilled (/home/ubuntu/c/node_modules/gaxios/build/src/gaxios.js:16:58)
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:228:7)
  response:
   { config:
      { url: 'https://www.googleapis.com/drive/v3/files?driveId=examplexyz&includeItemsFromAllDrives=true&corpa=drive&supportsAllDrives=true',
        method: 'GET',
        paramsSerializer: [Function],
        headers: [Object],
        params: [Object],
        validateStatus: [Function],
        responseType: 'json' },
     data: { error: [Object] },
     headers:
      { 'alt-svc': 'quic=":443"; ma=2592000; v="46,43",h3-Q048=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000',
        'cache-control': 'private, max-age=0',
        connection: 'close',
        'content-encoding': 'gzip',
        'content-type': 'application/json; charset=UTF-8',
        date: 'Thu, 24 Oct 2019 00:38:10 GMT',
        expires: 'Thu, 24 Oct 2019 00:38:10 GMT',
        server: 'GSE',
        'transfer-encoding': 'chunked',
        vary: 'Origin, X-Origin',
        'x-content-type-options': 'nosniff',
        'x-frame-options': 'SAMEORIGIN',
        'x-xss-protection': '1; mode=block' },
     status: 403,
     statusText: 'Forbidden' },
  config:
   { url: 'https://www.googleapis.com/drive/v3/files?driveId=examplexyz&includeItemsFromAllDrives=true&corpa=drive&supportsAllDrives=true',
     method: 'GET',
     paramsSerializer: [Function],
     headers:
      { 'Accept-Encoding': 'gzip',
        'User-Agent': 'google-api-nodejs-client/0.7.2 (gzip)',
        Authorization: 'Bearer something',
        Accept: 'application/json' },
     params:
      { driveId: 'examplexyz',
        includeItemsFromAllDrives: true,
        corpa: 'drive',
        supportsAllDrives: true },
     validateStatus: [Function],
     responseType: 'json' },
  code: 403,
  errors:
   [ { domain: 'global',
       reason: 'teamDriveIdRequiresTeamDriveCorpora',
       message: 'The driveId parameter must be specified if and only if corpora is set to drive.' } ] } undefined
close the queue { success: true }

最佳答案

您要从公共共享文件夹中检索文件列表。


您只想检索电子表格文件。

您想使用带有Node.js的googleapis实现此目的。
您已经能够使用Drive API的files.list方法检索文件列表。


如果我的理解是正确的,那么该修改如何?

修改点:


在此修改中,为了从公共共享文件夹中检索文件列表,对脚本的搜索查询进行了修改。


'folderId' in parents的搜索查询返回文件夹中的文件列表。



修改后的脚本:

const folderId = "###"; // Please set the folder ID of the publicly shared folder.

const drive = google.drive({ version: "v3", auth });
let fconf = {};
fconf.maxResults = 10;
fconf.orderBy = "createdTime";
fconf.q = `'${folderId}' in parents and mimeType = 'application/vnd.google-apps.spreadsheet'`;
drive.files.list(fconf, function(error, response) {
  if (error) {
    console.log(error);
  } else {
    console.log(response.data);
  }
});



如果要检索公共共享文件夹中所有文件的文件列表,请使用'${folderId}' in parents作为搜索查询。


注意:


在以上修改的脚本中,当文件夹不是您的文件夹且未公开共享时,无法从该文件夹中检索文件列表。请注意这一点。


参考文献:


Files: list
Search for Files


如果我误解了您的问题,而这不是您想要的结果,我深表歉意。

添加:

当您提供的文件夹ID用于脚本时,以下脚本如何?在这种情况下,在检索到的文件列表中可以看到8个文件夹。

示例脚本:

const drive = google.drive({ version: "v3", auth });
const folderId = "0B3d5a94e071mfjN6S19MeGZZYi1hWHNfeFlPWVdqS3RMWXhXMnhJeHhmdU91WWlwWjdCN1U";
let fconf = {};
fconf.maxResults = 10;
fconf.orderBy = "createdTime";
fconf.q = `'${folderId}' in parents`;
drive.files.list(fconf, function(error, response) {
  if (error) {
    console.log(error);
  } else {
    console.log(response.data);
  }
});

关于node.js - 谷歌驱动器列出文件在公共(public)共享文件夹中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58532683/

10-14 20:19
查看更多