authentication - 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
。
文档说:
string IBM.Cloud.SDK.Core.Authentication.Iam.IamAuthenticator.GetToken ( )
我都试过了getToken
和GetToken
。相同的错误信息。代码并不复杂,我做错了什么?
解决方案
这就是对我有用的最新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'));
推荐阅读
- sql - sql查询如何计算值在一张表中使用相同的列?
- php - 我正在尝试使用 rowCount 获取输入字段中的行,但图像失败
- javascript - 需要在 electron.js / 未捕获的错误:找不到模块
- python - 需要添加/删除 key-val,如果 key 不存在用于变量深度的嵌套字典
- xcode - 如何构建用于分发的颤振版本
- unix - 如何将日期格式的开始时间和以毫秒为单位的运行时间相加以在 unix 脚本中找到结束时间?
- android - 尝试检索 JSON 数组时,Retrofit2 GET 请求在列表中返回一些空条目
- python - 带有内部while循环的forloop中的python列表索引错误
- google-cloud-sql - 获取自动更新导致 100% 的 CPU 利用率
- react-native - react-native - 如何设置子视图以自动填充父母弯曲方向的“其他轴”?