繁体   English   中英

在Javascript / Node.Js中设置Watson Speech to Text API时出现问题

[英]Problems setting up Watson Speech to Text API in Javascript / Node.Js

我正在尝试将Watson Speech to Text设置为可在Node.Js应用程序中使用。

首先,我在app.js文件中设置服务器端以获取令牌(这是在设置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的示例)

然后,在实际的服务器端文件中,我具有以下代码来首先获取令牌并激活语音输入(以下示例 ):

              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错误,该错误应该得到令牌。

我究竟做错了什么? 哪里出错?

谢谢!

我能够重现您的问题-该示例似乎正在使用包含错误的较早版本的watson-developer-cloud

更新到最新版本的watson-developer-cloud (v3.12.0)为我解决了此问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM