这就是最新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直接用于语音转文本
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