【问题标题】:Is there a way to specifically target a shared drive when copying a file to a shared google drive using google drive-drive-api-v3 and node js使用 google drive-drive-api-v3 和 node js 将文件复制到共享的 google 驱动器时,有没有办法专门针对共享驱动器
【发布时间】:2020-03-26 10:04:10
【问题描述】:

我正在尝试通过 nodejs 使用驱动器 API v3 将谷歌文档上传到共享驱动器。进行上传的用户对将文件复制到的目标文件夹具有写入和编辑权限,并且所有 OAuth 都已设置等...但是,我收到以下错误:

   domain: 'global',
   reason: 'notFound',
   message: 'File not found: 1BPRlu4tWhsjsRp8tjQKL59o5apWkPa0k.',
   locationType: 'parameter',
   location: 'fileId' } ] }

有没有办法专门针对共享驱动器?

function listFiles(auth) {
  const drive = google.drive({version: 'v3', auth});
  drive.files.list({
    q: "mimeType = 'application/msword'",
    pageSize: 100,
    fields: 'nextPageToken, files(id, name)',
  }, (err, res) => {
    if (err) return console.log('The API returned an error: ' + err);
    const files = res.data.files;
    if (files.length) {
      console.log('Files:');
      files.map((file) => {
        console.log(`${file.name} (${file.id})`);
        drive.files.copy({
          fileId: file.id,
          'name' : 'Updated File Name',
          'mimeType' : 'application/vnd.google-apps.document'
          'parents': ['1BPRlu4tWhsjsRp8tjQKL59o5apWkPa0k']
        })
      });
    } else {
      console.log('No files found.');
    }
  });
}

【问题讨论】:

  • 1BPRlu4tWhsjsRp8tjQKL59o5apWkPa0k

标签: node.js google-drive-api google-docs


【解决方案1】:

根据 v3 标准,最好使用资源项来存储请求正文参数。使用 supportsAllDrives 标志,您可以指定针对个人驱动器和共享驱动器。通过这种方式,您也可以定位共享驱动器中的文件夹 ID。现在该参数默认为false,请注意此参数仅在2020年6月1日之前有效。之后假设所有应用程序都支持共享驱动器。

let resource = {
  'name' : 'Updated File Name',
  parents: ['1BPRlu4tWhsjsRp8tjQKL59o5apWkPa0k']
}
drive.files.copy({
    fileId: file.id,
    supportsAllDrives: true,
    resource: resource
});

参考资料:

Files copy docs

【讨论】:

    【解决方案2】:

    谢谢亚历山德罗。

    要真正让这个工作,我需要添加很多额外的参数,包括 driveId、语料库、includeItemsFromAllDrives 和 supportAllDrives。注意:supportAllDrives 即将被弃用(2020 年 6 月)。

    最后看起来有点像这样……

    function listFiles(auth) {
    
      const drive = google.drive({version: 'v3', auth});
    
      let resource = {
        name: 'Copied File Shared to Shared',
        parents: ['xxxxxxxxxxxxxxxxxxxxxxxxxxxx']. //folder to upload to on Shared Drive
      }
    
      drive.files.copy({
        fileId: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', //file to be copied
        driveId: 'xxxxxxxxxxxxxxxxx',
        includeItemsFromAllDrives: true,
        corpora: 'drive',
        supportsAllDrives: true,
        resource: resource
      }, (err, res) => {
        if (err) return console.log('The API returned an error: ' + err);
        const files = res.data.files;
        console.log("Success!!!")
      });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多