IBM Watson IAM令牌适合所有服务还是特定于每种服务(例如,语音转文本)? [英] Are IBM Watson IAM tokens good for all services or specific to each service, e.g., Speech-to-Text?
问题描述
IBM的文档说以下节点后端代码使您可以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.
IBM's documentation says that the following Node back end code enables you to 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
来获取令牌:
How do I get the token from speechToText
to pass to my front end Angular app running in the browser? I tried calling the method getToken
to get the 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
吗?
That didn't work. The error message is speechToText.getToken is not a function
. Should I try speechToText.authenticator.getToken
?
我尝试从ibm-watson/sdk
而不是ibm-watson/speech-to-text/v1
获取令牌?
I tried getting the token from ibm-watson/sdk
instead of from 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
.
That gets a smokin' new token. But the token doesn't work. When I run WatsonSpeech.SpeechToText.recognizeMicrophone
I get an error message HTTP Authentication failed; no valid credentials available
.
似乎每个IBM Watson服务都需要使用服务特定的URL创建的自己的令牌.我将语音转文本URL放入ibm-watson/sdk
,所以我应该获得正确的令牌.我不明白为什么令牌不起作用.
It appears that each IBM Watson service needs its own token, created with a service-specific URL. I put the Speech-to-Text URL into ibm-watson/sdk
so I should get the right token. I don't see why the token isn't working.
推荐答案
Take a look at the supplying credentials section of the README in the Node SDK about managing the token yourself if that's what you want to do:
如果要管理生命周期,请使用
BearerTokenAuthenticator
你自己.有关详细信息,请参阅对Watson服务进行身份验证.如果你想 要切换身份验证器,您必须覆盖身份验证器 财产.
Use the
BearerTokenAuthenticator
if you want to manage the lifecycle yourself. For details, see Authenticating to Watson services. If you want to switch your authenticator, you must override the authenticator property directly.
该身份验证"主题中有一个链接,可以帮助您了解访问过程.请参见调用IBM Cloud服务API
There's a link from that "Authenticating" topic that might help you understand the access process. See Invoking IBM Cloud service APIs
这篇关于IBM Watson IAM令牌适合所有服务还是特定于每种服务(例如,语音转文本)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!