Exporting file from google drive using googleapi in node.js

China☆狼群 提交于 2019-12-23 00:42:21

问题


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);
});

回答1:


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 :)




回答2:


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);


来源:https://stackoverflow.com/questions/49447186/exporting-file-from-google-drive-using-googleapi-in-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!