【问题标题】:IBM Watson WebSocket connection failure: "HTTP Authentication failed; no valid credentials available"IBM Watson WebSocket 连接失败:“HTTP 身份验证失败;没有可用的有效凭据”
【发布时间】:2026-01-27 02:25:01
【问题描述】:

我正在编写 IBM Watson Speech-to-text 教程。在“Using the WebSocket interface”部分的“打开连接并传递凭据”小节中,我复制了以下代码:

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 上的文档说,过期或无效的令牌将返回 401 错误,我没有得到,所以我认为我的令牌既没有过期也没有无效。有什么建议吗?

【问题讨论】:

    标签: websocket token ibm-watson


    【解决方案1】:

    我想你可以看到来自 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 - Watson Developer Cloud 中的 this 示例,使用 Javascript for Speech to Text。

    【讨论】:

      【解决方案2】:

      小学,我亲爱的华生!使用 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"
      

      【讨论】: