Reputation: 1217
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 #<IncomingMessage> 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.com/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.com/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.com/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);
}
})
Upvotes: 1
Views: 7567
Reputation: 1217
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 completely 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.com/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.com/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);
});
}
});
Upvotes: 4
Reputation: 41
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.com/davestevens/6f376f220cc31b4a25cd
Upvotes: 2
Reputation: 4568
You are passing the request response to the pipe
instead of the file you created.
Change the code .pipe(response);
to .pipe(file);
Upvotes: 0