【问题标题】:google drive listing files in public shared folder谷歌驱动器在公共共享文件夹中列出文件
【发布时间】:2019-10-24 00:48:24
【问题描述】:

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

这是我在 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 google-drive-api


    【解决方案1】:
    • 您想从公共共享文件夹中检索文件列表。
      • 您只想检索电子表格文件。
    • 您希望使用带有 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作为搜索查询。

    注意:

    • 在上面修改的脚本中,当文件夹不是你的并且没有公开共享时,无法从文件夹中检索文件列表。请注意这一点。

    参考资料:

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

    补充:

    当你提供的文件夹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);
      }
    });
    

    【讨论】:

    • 一流的反应。我现在收到 200 个响应,不幸的是 data.files 是一个空数组 []。有什么想法吗?公开的驱动器 url 是:drive.google.com/drive/folders/…
    • @user1709076 感谢您的回复。我带来的不便表示歉意。当我检查你提供的'folderId' in parents查询的文件夹时,发现所有文件的mimeType都是text/csv。所以请尝试fconf.q = `'${folderId}' in parents and mimeType = 'text/csv'`;fconf.q = `'${folderId}' in parents`;的查询。
    • 是的,正在尝试 fconf.q = '${folderId}' in parents - 我不知道为什么我得到一个空文件数组数据:{ kind: 'drive#fileList', incompleteSearch: false, files: [] } -无论我选择什么公用文件夹,似乎都是一个空文件数组,例如:fconf.q = '0B3d5a94e071mfjN6S19MeGZZYi1hWHNfeFlPWVdqS3RMWXhXMnhJeHhmdU91WWlwWjdCN1U' in parents;
    • @user1709076 感谢您的回复。我不得不再次道歉。关于文件夹ID,发现0B3d5a94e071mfjN6S19MeGZZYi1hWHNfeFlPWVdqS3RMWXhXMnhJeHhmdU91WWlwWjdCN1U和你评论的1IRg7eNuHNc7wmLkbpk__mNWc-MNtovLd的初始文件夹ID不同。在当前文件夹 ID 的情况下,所有文件都是文件夹。因此,您可以在 parents; 中检索带有fconf.q = '${folderId}' 的文件列表。但我不确定您当前的脚本。因此,我为您提供给我的答案的文件夹 ID 添加了示例脚本。你能确认一下吗?
    • 谢谢。是的,您是对的,通过使用googleapis.com/auth/drive.metadata.readonly,我可以查看其他人公开可用的驱动器文件-尽管我不明白,因为权限要求在接受时查看“您拥有的所有文件”。 googleapis.com/auth/drive.metadata.readonly 选项是选项中最不激进的策略。感谢您好心回答这个问题 - 我愿意接受您的回复。
    猜你喜欢
    • 1970-01-01
    • 2021-04-05
    • 2018-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-06
    相关资源
    最近更新 更多