IBM Watson WebSocket 连接失败:"HTTP Authentication failed; no valid credentials available"

IBM Watson WebSocket connection failure: "HTTP Authentication failed; no valid credentials available"

我正在编写 IBM Watson Speech-to-text 教程。在“Using the WebSocket interface”部分,"Opening a connection and passing credentials" 小节中,我复制了以下代码:

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...');

我收到一条错误消息:

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

我尝试对令牌进行硬编码:

var wsURI = 'wss://stream.watsonplatform.net/speech-to-text/api/v1/recognize?watson-token=Ln%2FV2...&model=es-ES_BroadbandModel';

同样的错误信息。

IBM 关于 tokens 的文档说,过期或无效的令牌将 return 出现 401 错误,但我没有得到,所以我认为我的令牌既没有过期也没有无效。有什么建议吗?

我想你可以看看 IBM Developers 的官方示例 here

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

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 Developers,我正在为我的项目使用并且运行良好。

您可以在代码行 #53 中看到,将监听设置为 true,否则它最终会超时并自动关闭 inactivity_timeout 适用于您发送音频时里面没有语音,当你根本不发送任何数据时就没有。

还有另一个示例,请参阅来自 IBM Watson 的 this 示例 - Watson Developer Cloud 使用 Javascript 进行语音转文本。

初级,我亲爱的华生!使用 IBM Watson 令牌需要注意三四件事。

首先,如果您使用 IBM 标识和密码,您将不会获得令牌。您必须使用为项目提供的用户名和密码。该用户名是一串带连字符的字母和数字。

其次,documentation for tokens 为您提供获取令牌的代码:

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"