Node.js使用用户名和密码从URL下载文件?

时间:2017-09-26 20:13:37

标签: node.js

从CLI我可以下载IBM Watson令牌并将其另存为curl -X GET --user username:password --output token "https://stream.watsonplatform.net/authorization/api/v1/token?url=https://stream.watsonplatform.net/speech-to-text/api" 文件:

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

var username = 'groucho';
var password = 'swordfish';

var header = { 
    // where does https go?
    Host: 'stream.watsonplatform.net',
    Path: '/authorization/api/v1/token?url=https://stream.watsonplatform.net/speech-to-text/api',
    Authorization: username:password
  };

var download = function(header, dest, cb) {
  var file = fs.createWriteStream("/javascript/services/token");
  var request = http.get(header, function(response) {
    response.pipe(file);
    file.on('finish', function() {
      file.close(cb);  // close() is async, call cb after close completes.
    });
  }).on('error', function(err) { // Handle errors
    fs.unlink(dest); // Delete the file async. (But we don't check the result)
    if (cb) cb(err.message);
  });

我如何在Node中执行此操作?我最好的猜测是:

https

当我为http设置节点时,问题是服务器是<html> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script> <script src="https://raw.githubusercontent.com/Microsoft/PowerBI-JavaScript/master/dist/powerbi.js"></script> <script type="text/javascript"> window.onload = function () { var models = window['powerbi-client'].models; var embedConfiguration = { type: 'dashboard', accessToken: {{access token}}, embedUrl: 'https://app.powerbi.com/dashboardEmbed?dashboardId={{dashboard id})' }; var $reportContainer = $('#dashboardContainer'); var report = powerbi.embed($reportContainer.get(0), embedConfiguration); } </script> <div id="dashboardContainer"></div> </html>吗?

2 个答案:

答案 0 :(得分:2)

如何将curl称为子进程?

const
    URL = "https://stream.watsonplatform.net/authorization/api/v1/token?url=https://stream.watsonplatform.net/speech-to-text/api",
    curlCommand = `curl -X GET --user username:password --output token ${URL}`,
    {
        exec
    } = require('child_process');

exec(curlCommand, (err, stdout, stderr) => {
            // your callback
});

答案 1 :(得分:2)

为了简化这一过程,您可以使用watson developer cloud package for speech to text以下是一个使用软件包的简单示例。这个例子直接来自文档

var SpeechToTextV1 = require('watson-developer-cloud/speech-to-text/v1');
var fs = require('fs');

var speech_to_text = new SpeechToTextV1({
  username: '<username>',
  password: '<password>'
});

var params = {
  // From file
  audio: fs.createReadStream('./resources/speech.wav'),
  content_type: 'audio/l16; rate=44100'
};

speech_to_text.recognize(params, function(err, res) {
  if (err)
    console.log(err);
  else
    console.log(JSON.stringify(res, null, 2));
});

// or streaming
fs.createReadStream('./resources/speech.wav')
  .pipe(speech_to_text.createRecognizeStream({ content_type: 'audio/l16; rate=44100' }))
  .pipe(fs.createWriteStream('./transcription.txt'));

现在,如果您真的只关心获取令牌而只使用请求的令牌,我建议使用request package

var request = require('request');
var username = 'username',
    password = 'password',
    url = 'https://' + username + ':' + password + '@stream.watsonplatform.net/authorization/api/v1/token?url=https://stream.watsonplatform.net/speech-to-text/api';

request({url: url}, function (error, response, body) {
   // Do more stuff with 'body' here
});