如何为 IBM Watson 的语音转文本服务网络套接字端点找到必要的访问网络令牌?

How to find necessary access web token for IBM Watson`s speech-to-text service web-socket endpoint?

我是 IBM Watson 服务的新手。我想测试语音转文本服务。我已经创建了新的 STT 服务并将其分配给创建的应用程序。根据 docs if I want to use STT web-socket endpoint it requires web-token for authentication. I found different articles on how to receive IBM tokens. The first says, that I need to create it from my API key but when I use that token I do not pass auth. Another way is to receive a token as was suggested here but for that, I need username and pass. Docs 的说法,我可以在服务凭证中找到它们。但是我在服务凭证 JSON 中没有这样的字段。

接下来是我的服务凭证:

{
  "apikey": "my_api_key",
  "iam_apikey_description": "description",
  "iam_apikey_name": "key_name",
  "iam_role_crn": "my_role_crn",
  "iam_serviceid_crn": "my_serviceid_crn",
  "url": "my_url_endpoint"
}

请帮助我弄清楚如何使用 api_key 为我的 STT 服务接收访问令牌。

You can access IBM Watson service APIs by using the API keys that were generated for the service instance. You use the API key to generate an IAM access token. You also use this process if you are developing an application that needs to work with other IBM Cloud services

因此,您需要执行 POST 请求 identity/token 方法,通过传递 API 密钥来生成 IAM 访问令牌。 cURL 的一个示例:

curl -k -X POST --header "Content-Type: application/x-www-form-urlencoded" --header "Accept: application/json" --data-urlencode "grant_type=urn:ibm:params:oauth:grant-type:apikey" --data-urlencode "apikey={your apikey}" "https://iam.bluemix.net/identity/token"

响应将类似于:

 "access_token": "eyJhbGciOiJIUz......sgrKIi8hdFs",
 "refresh_token": "SPrXw5tBE3......KBQ+luWQVY",
 "token_type": "Bearer",
 "expires_in": 3600,
 "expiration": 1473188353

一个调用 Watson 服务的示例:

curl -X GET \
--header "Authorization: Bearer {token}" \
"https://gateway.watsonplatform.net/discovery/api/v1/environments?version=2017-11-07"

Obs.:我建议您使用 Python SDK。 SDK 接受 API 密钥并管理令牌的生命周期,查看 this example,您只需要传递您的令牌即可。

  • 查看有关使用 Watson 服务的身份验证 IAM 的更多信息here