Search code examples
ibm-watsonspeech-to-text

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


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?


Solution

  • 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