IBM IAM IamAuthenticator getToken不是函数

我正在尝试获取令牌以在我的应用程序中使用IBM Watson Speech-to-Text。这是我的代码:

const { IamAuthenticator } = require('ibm-cloud-sdk-core');

const authenticator = new IamAuthenticator({
    apikey: 'myApiKey',});

  authenticator.getToken(function (err,token) {
    if (!token) {
      console.log('error: ',err);
    } else {
      // use token
    }
  });

错误消息是authenticator.getToken is not a function

documentation说:

string IBM.Cloud.SDK.Core.Authentication.Iam.IamAuthenticator.GetToken  (       )   

我已经尝试过getTokenGetToken。同样的错误信息。代码并不复杂,我在做什么错了?

love990311 回答:IBM IAM IamAuthenticator getToken不是函数

这就是最新ibm-watson node-sdk对我有用的东西,

使用此命令安装node-sdk

npm install --save ibm-watson

然后,在您的app.jsserver.js节点文件中使用此代码段以接收IAM访问令牌

const watson = require('ibm-watson/sdk');
const { IamAuthenticator } = require('ibm-watson/auth');

// to get an IAM Access Token
const authorization = new watson.AuthorizationV1({
  authenticator: new IamAuthenticator({ apikey: '<apikey>' }),url: ''
});

authorization.getToken(function (err,token) {
  if (!token) {
    console.log('error: ',err);
  } else {
    console.log('token: ',token);
  }
});

您还可以将IamAuthenticator直接用于语音转文本

const fs = require('fs');
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: 'https://stream.watsonplatform.net/speech-to-text/api/'
});

const params = {
  // From file
  audio: fs.createReadStream('./resources/speech.wav'),contentType: 'audio/l16; rate=44100'
};

speechToText.recognize(params)
  .then(response => {
    console.log(JSON.stringify(response.result,null,2));
  })
  .catch(err => {
    console.log(err);
  });

// or streaming
fs.createReadStream('./resources/speech.wav')
  .pipe(speechToText.recognizeUsingWebSocket({ contentType: 'audio/l16; rate=44100' }))
  .pipe(fs.createWriteStream('./transcription.txt'));
,

在您的other post中查看我的回答可能会有所帮助。如果要自己管理令牌认证过程,请使用BearerTokenAuthenticator

本文链接:https://www.f2er.com/2655125.html

大家都在问