[英]Node.js - Download File from Google Drive via request
I am trying to download a file from Google Drive using Node.js and the Request module. 我正在尝试使用Node.js和Request模块从Google云端硬盘下载文件。 I am getting the download link from the webContentLink area of the item metadata and these links work in my browser.
我从项目元数据的webContentLink区域获取下载链接,这些链接在我的浏览器中有效。
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 我正在使用在这里找到的方法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);
}
})
Okay this was wrong on a couple parts. 好的,这在某些方面是错误的。 First @fmodos was correct in passing the results to 'file' the stream I had created before.
首先,@ fmodos将结果传递到“文件”我之前创建的流中是正确的。
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);
});
}
});
这已经很老了,但是我只是遇到了问题,我提供了一个示例,说明了如何使用Google API Node.js库来使其正常工作: https : //gist.github.com/davestevens/6f376f220cc31b4a25cd
You are passing the request response to the pipe
instead of the file you created. 您正在将请求响应传递给
pipe
而不是传递给您创建的文件。
Change the code .pipe(response);
更改代码
.pipe(response);
to .pipe(file);
到
.pipe(file);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.