user1709076
user1709076

Reputation: 2856

google drive listing files in public shared folder

I have an authenticated google client and I am wanting to list files in someone else's public folder (i think the folder being shared or not is irrelevant since it is public)

Here is my code in 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);
  });
 }

note: there is no 'folderId' option in the documentation: https://developers.google.com/drive/api/v3/reference/files/list - just a driveId option

Although I set the corpa to "drive" and Drive Id, I get the following error

{ 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 }

Upvotes: 2

Views: 5281

Answers (1)

Tanaike
Tanaike

Reputation: 201603

  • You want to retrieve the file list from the publicly-shared folder.
    • You want to retrieve only Spreadsheet files.
  • You want to achieve this using googleapis with Node.js.
  • You have already been able to retrieve the file list with the method of files.list of Drive API.

If my understanding is correct, how about this modification?

Modification point:

  • In this modification, in order to retrieve the file list from the publicly shared folder, the search query of your script is modified.
    • The search query of 'folderId' in parents returns the file list in the folder.

Modified script:

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);
  }
});
  • If you want to retrieve the file list of all files in the publicly shared folder, please use '${folderId}' in parents as the search query.

Note:

  • In above modified script, when the folder is not yours and not shared publicly, the file list cannot be retrieved from the folder. Please be careful this.

References:

If I misunderstood your question and this was not the result you want, I apologize.

Added:

When the folder ID you provided is used for the script, how about the following script? In this case, 8 folders can be seen in the retrieved file list.

Sample script:

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);
  }
});

Upvotes: 2

Related Questions