2016-09-06 45 views
0

我試圖下載一個以前上傳的鬆弛文件。我正在使用以下鏈接,其中說我需要使用消息的url_private屬性以及授權標頭。 https://api.slack.com/types/file#authentication使用節點下載一個鬆弛文件

我試過這個,不能讓它工作。這是我目前的代碼。

var https = require('https'); 
 
var fs = require('fs'); 
 

 
var options = { 
 
    "method": "GET", 
 
    "hostname": "files.slack.com", 
 
    "path": "/files-pri/FOO/download/foo.jpg", 
 
    "rejectUnauthorized": "false", 
 
    "headers": { 
 
     "Authorization": "xoxp-foo-foo-foo" 
 
    } 
 
} 
 

 
function pDownload(url, dest){ 
 
    var file = fs.createWriteStream(dest); 
 
    return new Promise((resolve, reject) => { 
 
    var responseSent = false; // flag to make sure that response is sent only once. 
 

 
    https.get(options, response => { 
 
     response.pipe(file); 
 
     file.on('finish',() =>{ 
 
     file.close(() => { 
 
      if(responseSent) return; 
 
      responseSent = true; 
 
      resolve(); 
 
     }); 
 
     }); 
 
    }).on('error', err => { 
 
     if(responseSent) return; 
 
     responseSent = true; 
 
     reject(err); 
 
    }); 
 
    }); 
 
} 
 

 
//example 
 
pDownload(permalink, fileLocation) 
 
    .then(()=> console.log('downloaded file no issues...')) 
 
    .catch(e => console.error('error while downloading', e));

感謝您的幫助。

+0

你能不能發佈任何輸出信息和錯誤你得到,也許你的錯誤處理程序接收什麼? – broguinn

回答