0

我正在尝试获取令牌以在我的应用程序中使用 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

文档说:

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

我都试过了getTokenGetToken。相同的错误信息。代码并不复杂,我做错了什么?

4

2 回答 2

1

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

使用此命令安装 node-sdk

npm install --save ibm-watson

app.js然后,在您的或节点文件中使用此代码段server.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 与 Speech to Text 一起使用

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'));
于 2020-03-13T05:23:11.520 回答
1

在您的其他帖子中查看我的回答,这可能会有所帮助。BearerTokenAuthenticator如果您想自己管理令牌身份验证过程,请使用。

于 2020-03-16T11:30:20.610 回答