最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node.js - Download File from Google Drive via request - Stack Overflow

programmeradmin3浏览0评论

I am trying to download a file from Google Drive using Node.js and the Request module. I am getting the download link from the webContentLink area of the item metadata and these links work in my browser.

My request looks like this:

request.get({uri:item.webContentLink,headers:{authorization:'Bearer'+token}}).pipe(response);

My response is:

stream.js:79
dest.end();
     ^
TypeError: Object #<IningMessage> has no method 'end'
    at Request.onend (stream.js:79:10)
    at Request.EventEmitter.emit (events.js:117:20)

I am using the method I found here

Any help would be greatly appreciated.

**Edit Full Code

var request = require('request');

//getting children of folder
var url = "/"+id+"/children?maxResults=1000&q=trashed%3Dfalse&access_token="+token;;

request.get(url, function(err,response,body){

  detailsParse = JSON.parse(body);
  //if children are .mp3 then download

  if(detailsParse.mimeType == 'audio/mpeg'){
    var file = fs.createWriteStream("./"+detailsParse.title);

                    var getDown = "/"+detailsParse.id+"?access_token="+token;
                    request.get(getDown, function(err,response,body){
                        if(err){console.log(err)}

                        var downParse = JSON.parse(body);
                        request.get({uri:downParse.webContentLink,headers:{authorization:'Bearer'+token}}).pipe(response);
  }


})

I am trying to download a file from Google Drive using Node.js and the Request module. I am getting the download link from the webContentLink area of the item metadata and these links work in my browser.

My request looks like this:

request.get({uri:item.webContentLink,headers:{authorization:'Bearer'+token}}).pipe(response);

My response is:

stream.js:79
dest.end();
     ^
TypeError: Object #<IningMessage> has no method 'end'
    at Request.onend (stream.js:79:10)
    at Request.EventEmitter.emit (events.js:117:20)

I am using the method I found here https://github./google/google-api-nodejs-client/issues/150

Any help would be greatly appreciated.

**Edit Full Code

var request = require('request');

//getting children of folder
var url = "https://www.googleapis./drive/v2/files/"+id+"/children?maxResults=1000&q=trashed%3Dfalse&access_token="+token;;

request.get(url, function(err,response,body){

  detailsParse = JSON.parse(body);
  //if children are .mp3 then download

  if(detailsParse.mimeType == 'audio/mpeg'){
    var file = fs.createWriteStream("./"+detailsParse.title);

                    var getDown = "https://www.googleapis./drive/v2/files/"+detailsParse.id+"?access_token="+token;
                    request.get(getDown, function(err,response,body){
                        if(err){console.log(err)}

                        var downParse = JSON.parse(body);
                        request.get({uri:downParse.webContentLink,headers:{authorization:'Bearer'+token}}).pipe(response);
  }


})
Share Improve this question edited Jul 7, 2014 at 15:06 MonsterWimp757 asked Jul 3, 2014 at 18:30 MonsterWimp757MonsterWimp757 1,2172 gold badges16 silver badges29 bronze badges 4
  • What is response which you are piping to? – levi Commented Jul 3, 2014 at 18:53
  • I was trying to pipe the response the I am receiving from the request to file. runnable./UmhMG6nlm5cPAAAd/… This is another link where I thought this was possible. – MonsterWimp757 Commented Jul 3, 2014 at 19:00
  • Can you post the full code you are using? – levi Commented Jul 3, 2014 at 19:27
  • I have added the full request I am using – MonsterWimp757 Commented Jul 7, 2014 at 15:07
Add a ment  | 

3 Answers 3

Reset to default 4

Okay this was wrong on a couple parts. First @fmodos was correct in passing the results to 'file' the stream I had created before.

However this did not pletely solve the problem because I was running into authentication issues and issues not using the correct endpoint so the full answer is below

var request = require('request');

//getting children of folder
var url = "https://www.googleapis./drive/v2/files/"+id+"/children?maxResults=1000&q=trashed%3Dfalse&access_token="+token;

request.get(url, function(err,response,body){

  detailsParse = JSON.parse(body);
  //if children are .mp3 then download

  if(detailsParse.mimeType == 'audio/mpeg'){
    var file = fs.createWriteStream("./"+detailsParse.title);

    var getDown = "https://www.googleapis./drive/v2/files/"+detailsParse.id+"?access_token="+token;
    request.get(getDown, function(err,response,body){
      if(err){console.log(err)}

      var downParse = JSON.parse(body);
      //****THIS NEEDS TO BE THE downloadUrl ENDPOINT AND NOT THE webContentLink endpoint

      //*****NOTICE THE SPACE AFTER Bearer WAS MISSING BEFORE
      request.get({uri:downParse.downloadUrl,headers:{authorization:'Bearer '+token}}).pipe(response);
    });
  }


});

This is pretty old but I was just having issues with it, I've included an example of how I managed to get this working using the Google API Node.js library: https://gist.github./davestevens/6f376f220cc31b4a25cd

You are passing the request response to the pipe instead of the file you created.

Change the code .pipe(response); to .pipe(file);

发布评论

评论列表(0)

  1. 暂无评论