我正在尝试设置 Watson Speech to Text 以在我的 Node.Js 应用程序中工作。
首先,我在 app.js
文件中设置服务器端以获取 token (这是对设置 express
服务器的补充,我省略了该操作):
const AuthorizationV1 = require('watson-developer-cloud/authorization/v1');
const SpeechToTextV1 = require('watson-developer-cloud/speech-to-text/v1');
const TextToSpeechV1 = require('watson-developer-cloud/text-to-speech/v1');
const vcapServices = require('vcap_services');
// speech to text token endpoint
var sttAuthService = new AuthorizationV1(
Object.assign(
{
username: MY_IBM_APP_USERNAME, // or hard-code credentials here
password: MY_IBM_APP_PASSWORD
},
vcapServices.getCredentials('speech_to_text') // pulls credentials from environment in bluemix, otherwise returns {}
)
);
app.use('/api/speech-to-text/token', function(req, res) {
sttAuthService.getToken(
{
url: SpeechToTextV1.URL
},
function(err, token) {
if (err) {
console.log('Error retrieving token: ', err);
res.status(500).send('Error retrieving token');
return;
}
res.send(token);
}
);
});
(以上为 IBM Speech Javascript SDK 中的示例)
然后在实际的服务器端文件中,我有以下代码来首先获取 token 并激活语音输入(在 example here 之后):
fetch('/api/speech-to-text/token')
.then(function(response) {
return response.text();
}).then(function (token) {
var stream = WatsonSpeech.SpeechToText.recognizeMicrophone({
token: token,
outputElement: '#statement' // CSS selector or DOM Element to put the text in
});
stream.on('error', function(err) {
console.log(err);
});
document.querySelector('#stop').onclick = function() {
stream.stop();
};
}).catch(function(error) {
console.log(error);
});
但是,在 Chrome 上,我收到错误“无法构建 Web 套接字”,而在 Safari 上,我只是收到 /api/speech-to-text/token
的 404 错误,该错误应该获取 token 。
我做错了什么?哪里出错了?
谢谢!
最佳答案
我能够重现您的问题 - 该示例似乎使用的是包含错误的旧版本 watson-developer-cloud
。
更新到最新版本的 watson-developer-cloud
(v3.12.0) 解决了我的问题。
关于javascript - 在 Javascript/Node.Js 中设置 Watson Speech to Text API 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52778704/