首页 > 解决方案 > 调用 AuthorizationV1 获取语音转文本令牌时获取“必须设置错误验证器”

问题描述

运行节点 v10 ibm-watson v5.1.0

尝试获取语音到文本的令牌时出错。

#

{ "message": "Authenticator must be set.", "name": "Error", "stack": "Error: Authenticator must be set.\n at AuthorizationV1.BaseService (/nodejsAction/VuncPM95/node_modules/ibm-cloud -sdk-core/lib/base-service.js:66:19)\n 在新 AuthorizationV1 (/nodejsAction/VuncPM95/node_modules/ibm-watson/authorization/v1.js:44:28)\n 在 Object.token (/nodejsAction/VuncPM95/services/stt.js:17:32)\n 在 Speech-to-text_token (/nodejsAction/VuncPM95/index.js:42:54)\n 在 Object.exec (/nodejsAction/VuncPM95/ index.js:33:73)\n 在 Promise (/nodejsAction/VuncPM95/index.js:10:16)\n 在新 Promise ()\n 在 NodeActionRunner.main [as userScriptMain] (/nodejsAction/VuncPM95/index .js:9:12)\n 在 Promise (/nodejsAction/runner.js:73:35)\n 在新的 Promise ()" }

#

尝试使用 typescript 3.6.4 时

#

{ "message": "Authenticator must be set.", "name": "Error", "stack": "Error: Authenticator must be set.\n at te (eval at initializeActionHandler (/nodejsAction/runner.js:57 :23), :22:45665)\n 在新的 t (eval at initializeActionHandler (/nodejsAction/runner.js:57:23), :16:49145)\n 在 Object.token (eval at initializeActionHandler (/nodejsAction/ runner.js:57:23), :22:44594)\n 在 Speech-to-text_token (eval at initializeActionHandler (/nodejsAction/runner.js:57:23), :22:43617)\n 在 Object.exec (eval at initializeActionHandler (/nodejsAction/runner.js:57:23), :22:43498)\n 在 Promise (eval at initializeActionHandler (/nodejsAction/runner.js:57:23), :22:43038)\n在新的 Promise ()\n 在 NodeActionRunner。a [as userScriptMain] (eval at initializeActionHandler (/nodejsAction/runner.js:57:23), :22:43016)\n at Promise (/nodejsAction/runner.js:73:35)\n at new Promise () " }

#

export const SpeechToText = {
  token: (params: WatsonParams) => {
    const sttCredentials = Object.assign(
      {
        username: params.speechToTextUsername, // or hard-code credentials here
        password: params.speechToTextPassword,
        iam_apikey: params.speechToTextIamApikey, // if using an RC service
        url: params.speechToTextUrl ? params.speechToTextUrl : SpeechToTextV1.URL
      },
      vcapServices.getCredentials('speech_to_text') // pulls credentials from environment in bluemix, otherwise returns {}
    );
    const sttAuthService = new AuthorizationV1(sttCredentials);
    return Observable.create((observer) => {
      sttAuthService.getToken(function(err, response) {
        if (err) {
          console.log('Error retrieving token: ', err);
          observer.error('Error retrieving token...');
        } else {
          const token = response.token || response;
          if (params.speechToTextIamApikey) {
            observer.next({ accessToken: token, url: sttCredentials.url });
          } else {
            observer.next({ token: token, url: sttCredentials.url });
          }
          observer.complete();
        }
      });  
    });
  }  
}  

期望它返回一个令牌。

标签: ibm-watson

解决方案


在 v5 中更改了身份验证。请参阅迁移-V5

SDK 服务构造函数现在接受Authenticator用于验证请求的对象。构造函数不再接受像username和这样的个人凭据password

这是来自API 参考的示例。

const SpeechToTextV1 = require('ibm-watson/speech-to-text/v1');
const { IamAuthenticator } = require('ibm-watson/auth');

const speechToText = new SpeechToTextV1({
  authenticator: new IamAuthenticator({
    apikey: '{apikey}',
  }),
  url: '{url}',
});

推荐阅读