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

丶灬走出姿态 提交于 2019-12-19 04:35:40

问题


I am trying to use the Watson Speech To Text service which needs the following command for the websocket Interface as per the documentation

var token = {authentication-token};
var wsURI = 'wss://stream.watsonplatform.net/speech-to-text/api/v1/recognize'
+ '?watson-token=' + token
+ '&model=es-ES_BroadbandModel';

I have tried this to get the {authentication-token} using curl command on terminal

curl -X GET --user "apikey:{apikey}" "https://stream.watsonplatform.net/authorization/api/v1/token?url=https://stream.watsonplatform.net/text-to-speech/api"

The above command yields

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

Couldn't find proper documentation for this including several posts which seem to be out of scope after the recent changes made by IBM watson team.

Question: How do I get the authentication-token for connecting to the watson web socket properly?


回答1:


To get the authentication-token you need to run the following cURL command. This can be included in your program prior to the connection (websocket handshake).

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"

Follow this link for more details - https://console.bluemix.net/docs/services/watson/getting-started-iam.html

For C++ users - You may include this as below

#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);

inside callback take a variable token to hold the parsed response

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

This string should be used as the request header for websoket handshake




回答2:


As mentioned in the Speech to Text API docs,

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.

Here's the known limitation,

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.

If you have an old Speech to Text service with cloud foundry credentials (username and password), you can make a Curl request for the watson-token like this

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

Even this is documented in the API docs under watson-token parameter under 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.

Update: The WebSocket interface cannot be called from curl. Use a client-side scripting language to call the interface. You can find the code samples for streaming the audio in each of the programming languages SDKs like this example from Python SDK - https://github.com/watson-developer-cloud/python-sdk/blob/master/examples/microphone-speech-to-text.py

UPDATE 2019-02-09

According to the release documentation it is now again possible to authenticate to the websocket API from the browser. See: https://cloud.ibm.com/docs/services/speech-to-text/release-notes.html#release-notes for release 28 January 2019. However, getting an authentication token using your API key is NOT possible from the browser (prevented by CORG for security reasons). So to make this work: - get an api token for dallas (did not get it to work for other regions, looks like the lib is hardcoded set to the dallas server) - use a backend application to get an authentication token using your api key - use the authentication token straight from the browser using the speech-javascript-sdk



来源:https://stackoverflow.com/questions/53670666/how-to-get-the-authentication-token-for-ibm-watson-stt-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!