pythonaudiowebsocketstreamingibm-watson

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


I am new in working with IBM Watson`s services. I want to test the speech-to-text service. I have created the new STT service and assign it to the created app. According to the 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 say that I can find them in service credentials. But I have no such fields in service credentials JSON.

My service credentials looks next:

{
  "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"
}

Please help me to figure out how to receive an access token for my STT service with api_key.


Solution

  • 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

    So, you need to do a POST request identity/token method to generate an IAM access token by passing an API key. One example with 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"
    

    And the response will be something like:

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

    One example calling Watson Services:

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

    Obs.: I do suggest you use the Python SDK. The SDKs accept an API key and manage the lifecycle of the tokens, take a look in this example, you just need to pass your token.