IBM Watson WebSocket连接失败:“;HTTP认证失败;“没有可用的有效凭据”;

IBM Watson WebSocket连接失败:“;HTTP认证失败;“没有可用的有效凭据”;,websocket,token,ibm-watson,Websocket,Token,Ibm Watson,我正在做IBMWatson语音到文本的教程。在“”部分的“打开连接并传递凭据”小节中,我复制了以下代码: var token = watsonToken; console.log(token); // token looks good var wsURI = 'wss://stream.watsonplatform.net/speech-to-text/api/v1/recognize?watson-token=' + token + '&model=es-ES_BroadbandM

我正在做IBMWatson语音到文本的教程。在“”部分的“打开连接并传递凭据”小节中,我复制了以下代码:

var token = watsonToken;
console.log(token); // token looks good
var wsURI = 'wss://stream.watsonplatform.net/speech-to-text/api/v1/recognize?watson-token=' +
  token + '&model=es-ES_BroadbandModel';
var websocket = new WebSocket(wsURI);
websocket.onopen = function(evt) { onOpen(evt) };
websocket.onclose = function(evt) { onClose(evt) };
websocket.onmessage = function(evt) { onMessage(evt) };
websocket.onerror = function(evt) { onError(evt) };
我正在使用Angular,因此我为标记创建了一个值:

app.value('watsonToken', 'Ln%2FV...');
var wsURI = 'wss://stream.watsonplatform.net/speech-to-text/api/v1/recognize?watson-token=Ln%2FV2...&model=es-ES_BroadbandModel';
我收到一条错误消息:

WebSocket connection to 'wss://stream.watsonplatform.net/speech-to-text/api/v1/recognize?watson-toke...&model=es-ES_BroadbandModel' failed: HTTP Authentication failed; no valid credentials available
我尝试对令牌进行硬编码:

app.value('watsonToken', 'Ln%2FV...');
var wsURI = 'wss://stream.watsonplatform.net/speech-to-text/api/v1/recognize?watson-token=Ln%2FV2...&model=es-ES_BroadbandModel';
相同的错误消息


IBM关于的文档说,过期或无效的令牌将返回401错误,我没有得到这个错误,因此我假定我的令牌既没有过期也没有无效。有什么建议吗?

我想您可以看到IBM开发人员提供的官方示例

错误是因为身份验证无法正常工作。在发送识别请求之前,请尝试在此存储库中执行相同的步骤,如:

const QUERY_PARAMS_ALLOWED = ['model', 'X-Watson-Learning-Opt-Out', 'watson-token', 'customization_id'];

/**
 * pipe()-able Node.js Readable/Writeable stream - accepts binary audio and emits text in it's `data` events.
 * Also emits `results` events with interim results and other data.
 * Uses WebSockets under the hood. For audio with no recognizable speech, no `data` events are emitted.
 * @param {Object} options
 * @constructor
 */
function RecognizeStream(options) {
  Duplex.call(this, options);
  this.options = options;
  this.listening = false;
  this.initialized = false;
}
util.inherits(RecognizeStream, Duplex);

RecognizeStream.prototype.initialize = function() {
  const options = this.options;

  if (options.token && !options['watson-token']) {
    options['watson-token'] = options.token;
  }
  if (options.content_type && !options['content-type']) {
    options['content-type'] = options.content_type;
  }
  if (options['X-WDC-PL-OPT-OUT'] && !options['X-Watson-Learning-Opt-Out']) {
    options['X-Watson-Learning-Opt-Out'] = options['X-WDC-PL-OPT-OUT'];
  }

  const queryParams = extend({ model: 'en-US_BroadbandModel' }, pick(options, QUERY_PARAMS_ALLOWED));
  const queryString = Object.keys(queryParams)
    .map(function(key) {
      return key + '=' + (key === 'watson-token' ? queryParams[key] : encodeURIComponent(queryParams[key])); // our server chokes if the token is correctly url-encoded
    })
    .join('&');

  const url = (options.url || 'wss://stream.watsonplatform.net/speech-to-text/api').replace(/^http/, 'ws') + '/v1/recognize?' + queryString;

  const openingMessage = extend(
    {
      action: 'start',
      'content-type': 'audio/wav',
      continuous: true,
      interim_results: true,
      word_confidence: true,
      timestamps: true,
      max_alternatives: 3,
      inactivity_timeout: 600
    },
    pick(options, OPENING_MESSAGE_PARAMS_ALLOWED)
  );
这段代码来自IBM开发人员,对于我正在使用的项目,它工作得非常完美

您可以在代码行中看到,将“侦听”设置为“真”,否则它最终将超时,并在您发送音频时自动关闭,而不是在您根本不发送任何数据时


还有一个例子,请参阅IBM Watson-Watson开发者云使用Javascript进行语音到文本转换的例子。

我想您可以看到IBM开发者的官方例子

错误是因为身份验证无法正常工作。在发送识别请求之前,请尝试在此存储库中执行相同的步骤,如:

const QUERY_PARAMS_ALLOWED = ['model', 'X-Watson-Learning-Opt-Out', 'watson-token', 'customization_id'];

/**
 * pipe()-able Node.js Readable/Writeable stream - accepts binary audio and emits text in it's `data` events.
 * Also emits `results` events with interim results and other data.
 * Uses WebSockets under the hood. For audio with no recognizable speech, no `data` events are emitted.
 * @param {Object} options
 * @constructor
 */
function RecognizeStream(options) {
  Duplex.call(this, options);
  this.options = options;
  this.listening = false;
  this.initialized = false;
}
util.inherits(RecognizeStream, Duplex);

RecognizeStream.prototype.initialize = function() {
  const options = this.options;

  if (options.token && !options['watson-token']) {
    options['watson-token'] = options.token;
  }
  if (options.content_type && !options['content-type']) {
    options['content-type'] = options.content_type;
  }
  if (options['X-WDC-PL-OPT-OUT'] && !options['X-Watson-Learning-Opt-Out']) {
    options['X-Watson-Learning-Opt-Out'] = options['X-WDC-PL-OPT-OUT'];
  }

  const queryParams = extend({ model: 'en-US_BroadbandModel' }, pick(options, QUERY_PARAMS_ALLOWED));
  const queryString = Object.keys(queryParams)
    .map(function(key) {
      return key + '=' + (key === 'watson-token' ? queryParams[key] : encodeURIComponent(queryParams[key])); // our server chokes if the token is correctly url-encoded
    })
    .join('&');

  const url = (options.url || 'wss://stream.watsonplatform.net/speech-to-text/api').replace(/^http/, 'ws') + '/v1/recognize?' + queryString;

  const openingMessage = extend(
    {
      action: 'start',
      'content-type': 'audio/wav',
      continuous: true,
      interim_results: true,
      word_confidence: true,
      timestamps: true,
      max_alternatives: 3,
      inactivity_timeout: 600
    },
    pick(options, OPENING_MESSAGE_PARAMS_ALLOWED)
  );
这段代码来自IBM开发人员,对于我正在使用的项目,它工作得非常完美

您可以在代码行中看到,将“侦听”设置为“真”,否则它最终将超时,并在您发送音频时自动关闭,而不是在您根本不发送任何数据时


还有一个例子,请参阅IBM Watson-Watson开发者云使用Javascript进行语音到文本转换的例子。

初级,我亲爱的Watson!使用IBM Watson代币需要注意三到四件事

首先,如果使用IBMid和密码,您将无法获得令牌。您必须使用为项目提供的用户名和密码。该用户名是一个带有连字符的字母和数字字符串

第二,提供获取令牌的代码:

curl -X GET --user {username}:{password}
--output token
"https://stream.watsonplatform.net/authorization/api/v1/token?url=https://stream.watsonplatform.net/text-to-speech/api"
该代码的一部分隐藏在网页上,特别是说
/text-to-speech/
的部分。您需要将其更改为您想要使用的Watson产品或服务,例如,
/speech to text/
。令牌用于特定项目和特定服务

第三,代币在一小时内到期

最后,我必须加上反斜杠才能让代码在终端中运行:

curl -X GET --user s0921i-s002d-dh9328d9-hd923:wy928ye98e \
--output token \
"https://stream.watsonplatform.net/authorization/api/v1/token?url=https://stream.watsonplatform.net/speech-to-text/api"

我亲爱的华生!使用IBM Watson代币需要注意三到四件事

首先,如果使用IBMid和密码,您将无法获得令牌。您必须使用为项目提供的用户名和密码。该用户名是一个带有连字符的字母和数字字符串

第二,提供获取令牌的代码:

curl -X GET --user {username}:{password}
--output token
"https://stream.watsonplatform.net/authorization/api/v1/token?url=https://stream.watsonplatform.net/text-to-speech/api"
该代码的一部分隐藏在网页上,特别是说
/text-to-speech/
的部分。您需要将其更改为您想要使用的Watson产品或服务,例如,
/speech to text/
。令牌用于特定项目和特定服务

第三,代币在一小时内到期

最后,我必须加上反斜杠才能让代码在终端中运行:

curl -X GET --user s0921i-s002d-dh9328d9-hd923:wy928ye98e \
--output token \
"https://stream.watsonplatform.net/authorization/api/v1/token?url=https://stream.watsonplatform.net/speech-to-text/api"