首页 > 解决方案 > Google API - 未找到文件 id - 但返回的数据对应于我输入的 folderId

问题描述

我尝试通过 Google Drive API 将数据发送到 Google Drive。

我的预期结果是我尝试发送的文件到达我的谷歌驱动器文件夹。我在这个目标中输入了一个文件夹 ID。

通过Google Drive API的简单上传似乎实现了下载,但即使这样我也无法在数据库中找到我上传的文件。

我正在尝试使用Google drive API文档中的 Google create 功能。

事实是我输入了一个文件夹 ID。控制台返回我找不到文件 id,我不明白这种奇怪的行为。

这里控制台返回:

错误:在 createError _redirectable 处找不到文件:[Object],

[符号(outHeadersKey)]:[对象]},

数据:{错误:[对象]}},代码:404,错误:[{域:'全局',

原因:'notFound',消息:'文件未找到:1ZTCEjsqIH8NoB5xpo1AD32wBrV6bpsKI。',locationType:'parameter',位置:'fileId'}]}

到目前为止,我在Google 文档中找到了一些文档

404: File not found: {fileId} 用户没有文件的读权限,或者文件不存在。

{“错误”:{“错误”:[{“域”:“全局”,“原因”:“notFound”,“消息”:“找不到文件{fileId}”}],“代码”:404,“ message": "找不到文件:{fileId}" } }

这是我的 app.js :

// call the function
module.exports.insertDrive = function (req, auth) {
  console.log("callback reached.")

// folder ID 
var folderId = '0BwwA4oUTeiV1TGRPeTVjaWRDY1E';
// set the metadata of my file
var fileMetadata = {
  'name': req.body.word,
  parents:  [folderId]
};

// media type and path
var media = {
  mimeType: 'audio/*',
  body: fs.createReadStream(path.join(__dirname, "birds.mp3"))
};

// trying to create the file
drive.files.create({ // create error is probably link to drive.files.create
  auth: jwtClient,
  resource: fileMetadata,
  media: media,
  // returns only the file id
  fields: 'id' 
}, function (err, file) {
  if (err) {
    // Handle error
    console.error(err);
  } else {
    console.log('File Id: ', file.id);
  }
  console.log("callback accomplished.")
})};

谢谢

标签: javascriptnode.jsapigoogle-drive-api

解决方案


您可以查看本教程,它将向您解释 Google drive Api 的基础:

基本上,您必须确保您已发出良好的请求来检索 json Web 令牌。然后使用授权向谷歌驱动器上的文件夹发出请求。此外,请确保您已将目标文件夹与您创建的服务帐户的电子邮件共享。最后,要检索一些 id,您必须使用特定语法作为“file.data.id”。

这里的教程:

https://www.youtube.com/watch?v=gGSJpp6_ax0


推荐阅读