Node.js googleapis驱动器无法访问共享驱动器文件

Node.js googleapis驱动器无法访问共享驱动器文件,node.js,google-api,google-drive-api,Node.js,Google Api,Google Drive Api,我正在写一个脚本,应该能够通过文件名搜索共享驱动器文件 对于身份验证,我使用服务帐户 该服务帐户可以访问一个与我共享的Google Sheets文件和一个共享驱动器 我的代码当前如下所示: const drive=google.drive{version:'v3',auth}; const drives=wait drive.drives.list; console.logdrives.data.drives; const files=wait drive.files.list; console

我正在写一个脚本,应该能够通过文件名搜索共享驱动器文件

对于身份验证,我使用服务帐户

该服务帐户可以访问一个与我共享的Google Sheets文件和一个共享驱动器

我的代码当前如下所示:

const drive=google.drive{version:'v3',auth}; const drives=wait drive.drives.list; console.logdrives.data.drives; const files=wait drive.files.list; console.logfiles.data.files; 输出为:

[ { 种类:'drivedrive', id:'0AHOBXXXXXXXX', 名称:“共享_驱动器” } ] [ { 种类:'drivefile', id:'1LAPXWYRxxxxxxxxxxxxxxxxxxxx', 名称:“图纸文件”, mimeType:'application/vnd.google apps.spreadsheet' } ] 为什么我可以访问共享驱动器,但当我尝试访问所有文件时,我只得到共享驱动器之外的文件?共享驱动器包含多个目录,其中包含许多文件。为什么我看不见他们?服务帐户对共享驱动器和工作表文件都具有相同的查看器权限。

使用请求参数为空的驱动器API将只提供用户驱动器中的文件列表

如果要列出共享驱动器中的文件,需要配置以下参数:

corpora = drive
driveId = shared drive id
includeItemsFromAllDrives = true
supportsAllDrives = true
您还可以使用列出用户的共享驱动器。

使用带有空请求参数的驱动器API将只提供用户驱动器中的文件列表

如果要列出共享驱动器中的文件,需要配置以下参数:

corpora = drive
driveId = shared drive id
includeItemsFromAllDrives = true
supportsAllDrives = true

您还可以使用列出用户的共享驱动器。

请查看我从Google drive获取文件的实现。在这里,您可以找到通过分页获取文件、通过包含或排除共享文件进行过滤以及搜索功能的实现

import { google } from 'googleapis';


const getOauth2Client = () => new google.auth.OAuth2(
  process.env.GOOGLE_DRIVE_CLIENT_ID,
  process.env.GOOGLE_DRIVE_CLIENT_SECRET,
  process.env.GOOGLE_DRIVE_REDIRECT_URL
);


export const getFiles = async ({
  tokens, pageToken, keyword, notShared
}) => {
  const oauth2Client = getOauth2Client();
  oauth2Client.setCredentials(tokens);
  const drive = google.drive({
    version: 'v3',
    auth: oauth2Client
  });
  const options = {
    pageSize: 20,
    supportsAllDrives: false,
    includeItemsFromAllDrives: false,
    orderBy: 'createdTime desc',
    spaces: 'drive',
    corpora: 'user',
    q: "mimeType contains 'image/' and trashed = false",
    fields: 'nextPageToken, files(id, webContentLink, name, webViewLink, iconLink, thumbnailLink)',
    pageToken
  };
  if (keyword) options.q += ` and name contains '${keyword}'`;
  if (notShared) options.q += " and 'me' in writers and 'me' in owners";
  const res = await drive.files.list(options);
  if (res.status !== 200) throw new Error(res.statusText);
  return res;
};

请看一下我从Google drive获取文件的实现。在这里,您可以找到通过分页获取文件、通过包含或排除共享文件进行过滤以及搜索功能的实现

import { google } from 'googleapis';


const getOauth2Client = () => new google.auth.OAuth2(
  process.env.GOOGLE_DRIVE_CLIENT_ID,
  process.env.GOOGLE_DRIVE_CLIENT_SECRET,
  process.env.GOOGLE_DRIVE_REDIRECT_URL
);


export const getFiles = async ({
  tokens, pageToken, keyword, notShared
}) => {
  const oauth2Client = getOauth2Client();
  oauth2Client.setCredentials(tokens);
  const drive = google.drive({
    version: 'v3',
    auth: oauth2Client
  });
  const options = {
    pageSize: 20,
    supportsAllDrives: false,
    includeItemsFromAllDrives: false,
    orderBy: 'createdTime desc',
    spaces: 'drive',
    corpora: 'user',
    q: "mimeType contains 'image/' and trashed = false",
    fields: 'nextPageToken, files(id, webContentLink, name, webViewLink, iconLink, thumbnailLink)',
    pageToken
  };
  if (keyword) options.q += ` and name contains '${keyword}'`;
  if (notShared) options.q += " and 'me' in writers and 'me' in owners";
  const res = await drive.files.list(options);
  if (res.status !== 200) throw new Error(res.statusText);
  return res;
};

我相信你的目标如下

您希望使用带有googleapis for Node.js的驱动器API检索文件列表 在您的环境中,您拥有共享驱动器,并且您不仅要检索您的Google驱动器,还要检索共享驱动器的文件列表。 您已经能够使用驱动器API检索文件列表。 修改点: 为了从您自己的驱动器和共享驱动器中检索文件列表,需要为端点的查询参数设置几个特定参数。这一点我已经提过了。 从你的问题来看, 我认为您可能有多个共享驱动器。 为此,所有驱动器用于语料库。这样,就不需要设置每个驱动器ID。 您可能拥有超过1000个文件的文件。 为此,使用NextPageToken检索文件列表。 当上述各点反映到脚本中时,它将变成如下所示

修改脚本: 如果要从驱动器和共享驱动器中检索文件列表,可以使用以下脚本。在这种情况下,即使您有多个共享驱动器,也可以从所有共享驱动器和驱动器检索所有文件列表

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);
在这里,当您只想检索共享驱动器时,还可以按如下方式修改上述脚本

发件人:

致:

注: 如果要拆分驱动器和共享驱动器的文件列表,还可以使用以下脚本

  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);
参考:
我相信你的目标如下

您希望使用带有googleapis for Node.js的驱动器API检索文件列表 在您的环境中,您拥有共享驱动器,并且您不仅要检索您的Google驱动器,还要检索共享驱动器的文件列表。 您已经能够使用驱动器API检索文件列表。 修改点: 为了从您自己的驱动器和共享驱动器中检索文件列表,需要为端点的查询参数设置几个特定参数。这一点我已经提过了。 从你的问题来看, 我认为您可能有多个共享驱动器。 为此,所有驱动器用于语料库。这样,就不需要设置每个驱动器ID。 您可能拥有超过1000个文件的文件。 为此,使用NextPageToken检索文件列表。 当上述各点反映到脚本中时,它将变成如下所示

修改脚本: 如果要从驱动器和共享驱动器中检索文件列表,可以使用以下脚本。在这种情况下,即使您有多个共享驱动器,也可以从所有共享驱动器和驱动器检索所有文件列表

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);
在这里,当您只想检索共享驱动器时,还可以按如下方式修改上述脚本

发件人:

致:

注: 如果要拆分文件列表f 或者您的驱动器和共享驱动器,也可以使用以下脚本

  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);
参考:
非常感谢。这些选项正是我所缺少的。谢谢。这些选项是我所缺少的。谢谢你的详细解释。现在一切正常!谢谢你的详细解释。现在一切正常!谢谢你的例子!谢谢你的例子!