【问题标题】:Are IBM Watson IAM tokens good for all services or specific to each service, e.g., Speech-to-Text?IBM Watson IAM 令牌是否适用于所有服务或特定于每个服务,例如 Speech-to-Text?
【发布时间】: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


    【解决方案1】:

    查看 Node SDK 中 README 的提供凭据部分,了解如何自行管理令牌:

    如果您想管理生命周期,请使用 BearerTokenAuthenticator 你自己。详情请见Authenticating to Watson services。如果你想 要切换您的验证器,您必须覆盖验证器 直接属性。

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

    【讨论】:

      【解决方案2】:

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

      现在,要访问 IAM 控制的 Watson 服务之类的服务,您的用户名/密码或 API 密钥将转换为 Bearer 和 Refresh 令牌,Bearer 令牌仅在一定时间内有效,然后需要重新刷新令牌。这可能是您看到不同令牌的原因。

      你可能已经看到了底层的core Node.js SDK which has background information on Authentication 和一些函数。

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

      【讨论】:

        猜你喜欢
        • 2019-05-11
        • 2017-07-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多