使用节点

时间:2016-09-06 17:59:33

标签: javascript node.js slack

我试图下载之前上传的文件。我使用以下链接说我需要使用消息的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));




感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

根据您分享的链接上的文档,Authorization标头不正确。您需要Bearer YOUR_SLACK_TOKEN作为值,如下所示。

'Authorization': 'Bearer xoxp-foo-foo-foo-foo'

https://api.slack.com/types/file#authentication