繁体   English   中英

IBM Watson IAM 令牌适用于所有服务还是特定于每个服务,例如 Speech-to-Text?

[英]Are IBM Watson IAM tokens good for all services or specific to each service, e.g., Speech-to-Text?

IBM 的文档说,以下 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. 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 HTTP Authentication failed; no valid credentials available

似乎每个 IBM Watson 服务都需要自己的令牌,该令牌是使用特定于服务的 URL 创建的。 我将 Speech-to-Text URL 放入ibm-watson/sdk所以我应该得到正确的标记。 我不明白为什么令牌不起作用。

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

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

您可能已经看到底层核心 Node.js SDK,其中包含有关身份验证和一些功能的背景信息

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

如果您想这样做,请查看 Node SDK 中关于自己管理令牌的自述文件的提供凭据部分:

如果您想自己管理生命周期,请使用BearerTokenAuthenticator 有关详细信息,请参阅对 Watson 服务进行身份验证 如果要切换验证器,则必须直接覆盖验证器属性。

“身份验证”主题中有一个链接,可以帮助您了解访问过程。 请参阅 调用 IBM Cloud 服务 API

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM