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.
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获得令牌以传递给在浏览器中运行的前端角应用程序?我尝试调用方法getToken来获取令牌:
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创建。我把语音到文本的网址放到ibm-watson/sdk中,这样我就能得到正确的标记。我不明白为什么令牌不起作用了。
发布于 2020-03-16 11:28:03
看看Node中自述文件的提供凭据部分,如果您想这样做的话,您可以自己管理令牌:
如果您想自己管理生命周期,请使用
BearerTokenAuthenticator。有关详细信息,请参阅向Watson服务进行身份验证。如果要切换身份验证器,则必须直接覆盖身份验证器属性。
这个“身份验证”主题有一个链接,可以帮助您理解访问过程。请参阅调用IBM服务API
发布于 2020-03-15 12:56:04
IBM使用它所称的身份和访问管理(IAM)来管理对资源的访问。IAM有几个允许细粒度安全控制的概念。可以将范围访问权限授予用户或角色。因此,一个用户可能是资源的管理器,而另一个用户可能只是读取器。
现在,要访问像IAM控制的Watson服务这样的服务,您的用户名/密码或API密钥将被转换为Bearer和Refresh令牌,Bearer令牌只在一定时间内有效,然后需要一个新的刷新令牌。这可能是你看到不同标记的原因之一。
您可能已经看到了底层的具有身份验证背景信息的核心Node.js SDK和一些函数。
长话短说:当您成功创建IamAuthenticator时,您应该能够请求并使用令牌。更好的是,您可以将IamAuthenticator传递给许多服务,包括沃森服务,以初始化会话。代码“知道”如何获取身份验证信息,并使用它对其他服务进行身份验证。
https://stackoverflow.com/questions/60678364
复制相似问题