Diagathe Josué
Diagathe Josué

Reputation: 12076

Google API - file id not found - but the data returned correspond to the folderId I have entered

I try to sent data to Google drive trought the Google Drive API.

My expect outcome is that a file I try to sent reach my google drive folder. I have entered a folder ID in this goal.

With the simple upload of Google Drive API it seems the download is realized, but even with that I can't find my uploaded file in the database after.

I'm trying to use the Google create function from Google drive API documentation.

The fact is that I have entered a folder ID. The console returns me that File id is not found, I don't understand this strange behavior.

Here the console returns :

Error: File not found at createError _redirectable: [Object],

[Symbol(outHeadersKey)]: [Object] },

data: { error: [Object] } }, code: 404, errors: [ { domain: 'global',

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

So far, I have found some documentation on Google documentation :

404: File not found: {fileId} The user does not have read access to a file, or the file does not exist.

{ "error": { "errors": [ { "domain": "global", "reason": "notFound", "message": "File not found {fileId}" } ], "code": 404, "message": "File not found: {fileId}" } }

Here my 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.")
})};

Thanks

Upvotes: 0

Views: 5014

Answers (1)

HoCo_
HoCo_

Reputation: 1372

You can check this tutorial wich will explain you the bases of Google drive Api :

Basically, you have to ensure that you have make a good request to retrieve a json web token. Then use the authorization to make a request to a folder on google drive. Also, ensure that you have shared the folder you target with the email of the service account that you have created. Finally, to retrieve some id you have to use a specific syntax as "file.data.id".

Here the tutorial :

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

Upvotes: 3

Related Questions