javascript - Google DRIVE API V3 - 使用 nodejs 获取根文件夹 ID

标签 javascript node.js google-drive-api google-api-nodejs-client google-drive-shared-drive

我需要获取根文件夹 ID 并过滤以根文件夹为父文件夹的文件夹。使用 Nodejs 和 google drive API v3 。 这是我的代码以及如何更改此代码以获取根文件夹 ID?

const service = google.drive('v3');
			service.files.list({
			auth: client2,
			fields: 'nextPageToken, files(id, name, webContentLink, webViewLink, mimeType, parents)'
		  	}, (err, res) => {
			if (err) {
			  console.error('The API returned an error.');
			  throw err;
			}
			const files = res.data.files;
			if (files.length === 0) {
			  console.log('No files found.');
			} else {
				
			  console.log('Files Found!');
			  for (const file of files) {
			  		console.log(`${file.name} (${file.id})`);

			  }

最佳答案

根文件夹的文件夹 ID 是“root”。 file.list 方法有一个名为 q 的选项参数用于搜索。

 service.files.list({auth: auth,
    resource: { parents: [ folderId ] },
    q: "'root' in parents",
    fields: '*',
    spaces: 'drive',
    pageToken: pageToken,
  }

这段代码是一种猜测,因为我没有能力在这台机器上测试 Node 。

关于javascript - Google DRIVE API V3 - 使用 nodejs 获取根文件夹 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49892343/

相关文章:

javascript - 处理 `keyup` 事件时出错

node.js - Mongoose 填充 ObjectId 引用或字符串

node.js - 如何使用 "find where"SailsJS 蓝图路由?

c# - 如何使用 OAuth 在 C# 中创建新的 Google Drive 服务

csv - 如何从已上传到我们的 Google 云端硬盘的 CSV 自动导入和替换 Google 表格中的数据?

javascript - 初学者帮助 - 此代码属于何处?

javascript - 防止光标更改为手动禁用按钮

javascript - Nodejs 和 Express 中的动态配置文件

java - 带有回调的方法中的操作顺序不正确

javascript - 防止空格键关闭确认框