【发布时间】:2020-03-13 23:24:51
【问题描述】:
IBM 的 documentation 表示,以下 Node 后端代码使您能够 Use the API key to have the SDK manage the lifecycle of the token. The SDK requests an access token, ensures that the access token is valid, and refreshes it if necessary.
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}',
});
如何从speechToText 获取令牌以传递给在浏览器中运行的前端 Angular 应用程序?我尝试调用getToken方法获取token:
const SpeechToTextV1 = require('ibm-watson/speech-to-text/v1');
const { IamAuthenticator } = require('ibm-watson/auth');
const speechToText = new SpeechToTextV1({
authenticator: new IamAuthenticator({
apikey: 'my-api-key',
}),
url: 'my-url',
});
speechToText.getToken(function (err, token) {
if (!token) {
console.log('error: ', err);
} else {
console.log(token);
// do more stuff with the token
}
});
那没用。错误消息是speechToText.getToken is not a function。我应该试试speechToText.authenticator.getToken 吗?
我尝试从 ibm-watson/sdk 而不是从 ibm-watson/speech-to-text/v1 获取令牌?
const watson = require('ibm-watson/sdk');
const { IamAuthenticator } = require('ibm-watson/auth');
const authorization = new watson.AuthorizationV1({
authenticator: new IamAuthenticator({ apikey: 'my-api-key' }),
url: 'my-url'
});
authorization.getToken(function (err, token) {
if (!token) {
console.log('error: ', err);
} else {
console.log(token);
// do stuff with token
}
});
这会得到一个冒烟的新令牌。但是令牌不起作用。当我运行WatsonSpeech.SpeechToText.recognizeMicrophone 时,我收到一条错误消息HTTP Authentication failed; no valid credentials available。
似乎每个 IBM Watson 服务都需要自己的令牌,该令牌是使用特定于服务的 URL 创建的。我将 Speech-to-Text URL 放入 ibm-watson/sdk,所以我应该得到正确的令牌。我不明白为什么令牌不起作用。
【问题讨论】:
标签: ibm-cloud ibm-watson speech-to-text ibm-iam