1

I am using googleapi to export the file from a google drive. I have the following code in my export.js. When I run this file, even though i have given the mimeType it throws an error saying "The API returned an error: Error: Required parameter: mimeType"

var drive = google.drive({
  version: 'v3',
  auth: auth
});

var dest = fs.createWriteStream('./public/data/myfile.txt');
drive.files.export({
  fileId : fileID,
  mimeType : 'text/plain'
}, function(err, response) {
  if (err) {
  console.log('The API returned an error: ' + err);
  return;
  }
  console.log('Received %d bytes', response.length);
  fs.writeFileSync(dest, response);
});
Reshma
  • 37
  • 2
  • 6
  • I have same problem, I have make an issue for it as well: https://github.com/google/google-api-nodejs-client/issues/1098 – Luckylooke Mar 31 '18 at 18:49

2 Answers2

2

Ohai! Maintainer here. When something like this happens, it's usually because there's some mismatch between the version of googleapis and google-auth-library. As far as the code is concerned, we have a full working copy here:

https://github.com/google/google-api-nodejs-client/blob/master/samples/drive/export.js

Now - on what to do. You need to make sure that a.) you are using the latest version of googleapis, and b.) you DO NOT have google-auth-library in your package.json. Please try:

$ npm uninstall --save google-auth-library $ npm install --save googleapis@28

Give that a shot and let me know how it goes :)

Justin Beckwith
  • 7,686
  • 1
  • 33
  • 55
  • for me it was updating packages as Justin suggested and do some little changes to code.. more info here: https://github.com/google/google-api-nodejs-client/issues/1098, thank you @justin-beckwith for your help – Luckylooke Apr 01 '18 at 10:26
0

I think you can try it this way and make sure your file matches the mimeType you are requesting and maybe you need to say that you are accepting binary data.

drive.files.export({
   fileId: 'asxKJod9s79', // A Google Doc pdf
   mimeType: 'application/pdf'},
   { encoding: null }, // Make sure we get the binary data
   function (err, buffer) {
       // ...
       // convert and write the file
    });

Note that the API returns a buffer object and you need to convert and write it to a file. Refer this.

Or you can use this :

var fileId = '1ZdR3L3qP4Bkq8noWLJHSr_iBau0DNT4Kli4SxNc2YEo';
var dest = fs.createWriteStream('/tmp/resume.pdf');
drive.files.export({
  fileId: fileId,
  mimeType: 'application/pdf'
})
    .on('end', function () {
      console.log('Done');
    })
    .on('error', function (err) {
      console.log('Error during download', err);
    })
    .pipe(dest);
damitj07
  • 2,689
  • 1
  • 21
  • 40
  • 1
    Thanks for the reply. I have tried these options as well. It didn't work for me. I get the same error "Error: Required parameter: mimeType" – Reshma Mar 23 '18 at 11:11