IBM Watson IAM令牌是对所有服务有用还是特定于每个服务,例如,语音转文本?

问题描述 投票:1回答:2

IBM的documentation说下面的节点后端代码使您可以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以获得令牌:

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,因此我应该获得正确的令牌。我不明白为什么令牌不起作用。

ibm-cloud ibm-watson speech-to-text ibm-iam
2个回答
0
投票

IBM Cloud使用它所谓的Identity and Access Management (IAM)来管理对资源的访问。 IAM具有几个允许进行细粒度安全控制的概念。您可以为用户或角色授予作用域访问权限。因此,一个用户可以是资源的管理者,而另一用户只能是读者。

<< 。这可能是您看到不同令牌的原因。

您可能已经看过底层的core Node.js SDK which has background information on Authentication和一些功能。

长话短说:成功创建IamAuthenticator后,您应该能够请求令牌并使用它。更好的是,您可以将IamAuthenticator传递给许多服务(包括Watson服务)以初始化会话。代码“知道”如何获取身份验证信息并将其用于其他服务的身份验证。


0
投票
[如果要这样做,请查看Node SDK中README的提供凭据部分,有关如何亲自管理令牌:

如果要管理生命周期,请使用BearerTokenAuthenticator你自己有关详细信息,请参见Authenticating to Watson services。如果你想要切换身份验证器,您必须覆盖身份验证器财产。

该“身份验证”主题中有一个链接,可能会帮助您了解访问过程。参见Invoking IBM Cloud service APIs

© www.soinside.com 2019 - 2024. All rights reserved.