Node.js - 通过请求从Google云端硬盘下载文件

时间:2014-07-03 18:30:57

标签: javascript node.js google-drive-api

我正在尝试使用Node.js和请求模块从Google云端硬盘下载文件。 我从项目元数据的webContentLink区域获取下载链接,这些链接在我的浏览器中工作。

我的请求如下:

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

我的回答是:

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)

我使用的是https://github.com/google/google-api-nodejs-client/issues/150

中找到的方法

非常感谢任何帮助。

**编辑完整代码

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


})

3 个答案:

答案 0 :(得分:4)

好的,这对几个部分来说都是错误的。 首先将@fmodos传递给&#39;文件&#39;我之前创建的流。

然而,这并没有完全解决问题,因为我遇到了身份验证问题并且问题没有使用正确的端点,所以完整的答案在下面

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


});

答案 1 :(得分:2)

这已经很老了,但我只是遇到了问题,我已经提供了一个示例,说明了如何使用Google API Node.js库实现这一点:https://gist.github.com/davestevens/6f376f220cc31b4a25cd

答案 2 :(得分:0)

您正在将请求响应传递给pipe,而不是您创建的文件。

将代码.pipe(response);更改为.pipe(file);

相关问题