代码之家  ›  专栏  ›  技术社区  ›  Thomas David Kehoe

节点。js从带有用户名和密码的URL下载文件?

  •  1
  • Thomas David Kehoe  · 技术社区  · 8 年前

    我可以从CLI下载IBM Watson令牌并将其保存为文件 token

    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"
    

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

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

    问题是服务器 https http

    2 回复  |  直到 8 年前
        1
  •  2
  •   Andrew Lohr    8 年前

    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
    });
    
        2
  •  2
  •   Kristianmitk    8 年前

    打电话怎么样 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
    });
    
    推荐文章