如何获取 IBM watson STT 服务的身份验证令牌?

How to get the authentication token for IBM watson STT service?

我正在尝试使用 Watson Speech To Text 服务,该服务需要根据 documentation

对 websocket 接口执行以下命令
var token = {authentication-token};
var wsURI = 'wss://stream.watsonplatform.net/speech-to-text/api/v1/recognize'
+ '?watson-token=' + token
+ '&model=es-ES_BroadbandModel';

我试过在终端

上使用 curl 命令获取 {authentication-token}
curl -X GET --user "apikey:{apikey}" "https://stream.watsonplatform.net/authorization/api/v1/token?url=https://stream.watsonplatform.net/text-to-speech/api"

以上命令产生

Error: {"code":401,"error":"Unauthorized","description":"ERCD250-LDAP-DN-AUTHERR"}

找不到合适的文档,包括在 IBM watson 团队 recent changes 之后似乎超出范围的几篇文章。

问题:如何获取 authentication-token 以正确连接到 watson web 套接字?

Speech to Text API 文档中所述,

You cannot use JavaScript to call the WebSocket interface from a browser. The watson-token parameter that is available with the /v1/recognize method does not accept IAM tokens or API keys. For information about working around this limitation, see the Release notes.

这是已知的限制,

The Speech to Text service has the following known limitation.

Service instances that use IAM authentication cannot currently use JavaScript to call the Speech to Text WebSocket interface. This limitation applies to any application (such as the service demo) that uses JavaScript to make WebSocket calls from a browser. WebSocket calls that are made with other languages can use IAM tokens or API keys. To work around this limitation, you can do the following:

Call the WebSocket interface from outside of a browser. You can call the interface from any language that supportsthe WebSockets. Refer to information in The WebSocket interface for guidance when working with another language.

The Watson SDKs provide the simplest way to call the WebSocket interface from another language. The SDKs accept an API key and manage the lifecycle of the tokens. For information about using the WebSocket interface with the Node.js, Java, Python, and Ruby SDKs, see the API reference.

Use the synchronous or asynchronous HTTP interfaces to perform speech recognition.

如果您有一个带有 cloud foundry 凭证(用户名和密码)的旧 Speech to Text 服务,您可以像这样 watson-token 发出 Curl 请求

curl -u {USERNAME}:{PASSWORD}"https://stream.watsonplatform.net/authorization/api/v1/token?url=https://stream.watsonplatform.net/speech-to-text/api"

即使这在 API 文档中的 watson-token 参数下的 Curl

中也有记录

Provides an authentication token for the service. Use a token that is based on Cloud Foundry service credentials. Do not pass an IAM access token or API key with this parameter.

更新: WebSocket接口无法从curl调用。使用客户端脚本语言调用接口。您可以在每个编程语言 SDK 中找到用于流式传输音频的代码示例,例如 Python SDK - https://github.com/watson-developer-cloud/python-sdk/blob/master/examples/microphone-speech-to-text.py

更新 2019-02-09

根据发布文档,现在可以再次从浏览器对 websocket API 进行身份验证。请参阅:https://cloud.ibm.com/docs/services/speech-to-text/release-notes.html#release-notes 2019 年 1 月 28 日发布。 但是,无法从浏览器使用 API 密钥获取身份验证令牌(出于安全原因被 CORG 阻止)。 所以要使这项工作: - 为达拉斯获取一个 api 令牌(没有让它在其他地区工作,看起来 lib 被硬编码设置为达拉斯服务器) - 使用后端应用程序使用您的 api 密钥获取身份验证令牌 - 使用 speech-javascript-sdk

直接从浏览器使用身份验证令牌

要获得 authentication-token,您需要 运行 以下 cURL 命令。这可以在连接(websocket 握手)之前包含在您的程序中。

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"

关注此 link 了解更多详情 - https://console.bluemix.net/docs/services/watson/getting-started-iam.html

对于 C++ 用户 - 您可以将其包含在下面

#include <curl/curl.h>

main(){
    //step 1- Initialise curl library
    //step 2- Set header
              curl_slist_append(headers,"Accept: application/json");
    //step 3- Set Post request data
              curl_slist_append(postdata,"grant_type=urn:ibm:params:oauth:grant-type:apikey&apikey={my apikey}");
    //step 4- setup cURL request
          curl_easy_setopt(curl, CURLOPT_URL,"https://iam.bluemix.net/identity/token");
          curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
          curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postdata);
/*Write callbacks to use the response in your authentication*/

          res = curl_easy_perform(curl);

    curl_slist_free_all(headers);
    // always cleanup
    curl_easy_cleanup(curl);

callback 中取一个变量 token 来保存解析后的响应

token = "Bearer<space><ParsedResponse>";

此字符串应用作请求header websoket 握手