2017-09-26 62 views
0

從CLI我可以下載的IBM沃森令牌,並將其保存爲文件tokenNode.js從一個帶有用戶名和密碼的URL下載文件?

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

它是一個問題,問題是,服務器https當我有節點設置爲http

+0

你爲什麼不使用獲得令牌,只有令牌'https'模塊? – Joe

回答

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

'請求'看起來不錯,但我得到錯誤消息「錯誤:無法找到模塊'請求'」。我用'npm install request -g'和'npm list --depth = 0 -g'顯示'[email protected]'來安裝它。我很難... –

+0

IBM Watson Cloud SDK看起來不錯,但我不確定如何從AngularJS調用「SpeechToTextV1」。我有一行'var stream = WatsonSpeech.SpeechToText.recognizeMicrophone({'我會用'var stream = WatsonSpeech.SpeechToTextV1.recognizeMicrophone替換它({'? –

+0

')您將從後端調用watson雲SDK。是一個前端框架 –

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

很簡單!謝謝! –

相關問題