Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
272 views
in Technique[技术] by (71.8m points)

node.js - googleapis drive unable to access shared drive files

I am writing a script that should be able to search through shared drive files by file name.

For authentication, I use a Service Account.

The Service Account has an access to a Google Sheets file, that is shared with me and a shared drive.

My code currently looks like this:

  const drive = google.drive({ version: 'v3', auth });
  const drives = await drive.drives.list();
  console.log(drives.data.drives);
  const files = await drive.files.list();
  console.log(files.data.files);

The output is:

[
  {
    kind: 'drive#drive',
    id: '0AHOBXXXXXXXX',
    name: 'Shared_drive'
  }
]
[
  {
    kind: 'drive#file',
    id: '1LAPXWyRXXXXXXXXXXXXXXXXXXXXXXX',
    name: 'Sheets File',
    mimeType: 'application/vnd.google-apps.spreadsheet'
  }
]

Why am I able to access the shared drive, but when I try to access all the files I only get Sheets File (which is outside the shared drive)? The shared drive contains multiple directories with many files in them. Why can't I see them? The Service Account has the same viewer permissions on both - shared drive and Sheets file.

question from:https://stackoverflow.com/questions/66048909/googleapis-drive-unable-to-access-shared-drive-files

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I believe your goal as follows.

  • You want to retrieve the file list using Drive API with googleapis for Node.js
  • In your environment, you have the shared Drive, and you want to retrieve the file list not only your Google Drive, but also the shared Drive.
  • You have already been able to retrieve the file list using Drive API.

Modification points:

  • In order to retrieve the file list from both your own drive and the shared drive, it is required to set the several specific parameters for the query parameter of the endpoint. This has already been mentioned by Ron M's answer.
  • From your question,
    • I thought the possibility that you have several shared Drives.
      • For this, allDrives is used for corpora. By this, it is not required to be set each drive ID.
    • You might have the files of more than 1000 files.
      • For this, the file list is retrieved using NextPageToken.

When above points are reflected to your script, it becomes as follows.

Modified script:

When you want to retrieve the file list from both your drive and the shared drive, you can use the following script. In this case, even when you have several shared Drives, all file list can be retrieved from all shared drives and your drive.

const drive = google.drive({ version: "v3", auth });
const fileList = [];
let NextPageToken = "";
do {
  const params = {
    pageToken: NextPageToken || "",
    pageSize: 1000,
    fields: "nextPageToken, files(id, name)",
    corpora: "allDrives",
    includeItemsFromAllDrives: true,
    supportsAllDrives: true,
  };
  const res = await drive.files.list(params);
  Array.prototype.push.apply(fileList, res.data.files);
  NextPageToken = res.data.nextPageToken;
} while (NextPageToken);
console.log(fileList);
  • Here, when you want to retrieve only the shared Drive, you can also modify above script as follows.

    • From:

        Array.prototype.push.apply(fileList, res.data.files);
      
    • To:

        Array.prototype.push.apply(fileList, res.data.files.filter((f) => f.driveId));
      

Note:

  • If you want to split the file list for your drive and shared drives, you can also use the following script.

      const drive = google.drive({ version: "v3", auth });
      const fileList = { myDrive: [], sharedDrives: {} };
      let NextPageToken = "";
      do {
        const params = {
          pageToken: NextPageToken || "",
          pageSize: 1000,
          fields: "nextPageToken, files(id, name, driveId)",
          corpora: "allDrives",
          includeItemsFromAllDrives: true,
          supportsAllDrives: true,
        };
        const res = await drive.files.list(params);
        res.data.files.forEach((f) => {
          if (f.driveId) {
            fileList.sharedDrives[f.driveId] = fileList.sharedDrives[f.driveId]
              ? fileList.sharedDrives[f.driveId].concat(f)
              : [f];
          } else {
            fileList.myDrive = fileList.myDrive.concat(f);
          }
        });
        NextPageToken = res.data.nextPageToken;
      } while (NextPageToken);
      console.log(fileList);
    

Reference:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...