Search code examples
javaibm-watsonspeech-to-textibm-iam

How authorize IBM Cloud Watson service using IAM authorization


I am trying to authorise my IBM Cloud speech-to-text service (IAM authorisation) via the IBM cloud Java SDK. Though my API key and service url are correct I am still getting 401 not authorized from the server.

This is my code:

IamOptions iamOptions = new IamOptions.Builder().apiKey("dummy-api-key").url("dummy-url").build();
SpeechToText speechToText = new SpeechToText(iamOptions);

This is the server response (logs)

Aug. 09, 2018 10:44:01 PM okhttp3.internal.platform.Platform log
INFO: --> POST https://iam.bluemix.net/identity/token h2 (135-byte body)
Aug. 09, 2018 10:44:02 PM okhttp3.internal.platform.Platform log
INFO: <-- 200  https://iam.bluemix.net/identity/token (181ms, 1759-byte body)
Aug. 09, 2018 10:44:03 PM com.ibm.watson.developer_cloud.speech_to_text.v1.websocket.BaseRecognizeCallback onError
SEVERE: Expected HTTP 101 response but was '401 Not Authorized'
java.net.ProtocolException: Expected HTTP 101 response but was '401 Not Authorized'
    at okhttp3.internal.ws.RealWebSocket.checkResponse(RealWebSocket.java:216)
    at okhttp3.internal.ws.RealWebSocket$2.onResponse(RealWebSocket.java:183)
    at okhttp3.RealCall$AsyncCall.execute(RealCall.java:141)
    at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1135)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
    at java.base/java.lang.Thread.run(Thread.java:844)

I have not included my speech synthesis code since it's irrelevant. I have checked the IBM Java docs and the IAM docs. This is how they instruct to authorize in IBM Cloud.

IBM GitHub page example


Solution

  • I got it working. You don't pass an url parameter, instead set it as an end-point.

    IamOptions iamOptions = new IamOptions.Builder().apiKey("ElPOiM4sMnO4CzJq7LEx1h-4dgaQcCNS1WgGYsnNi59D").build();
    SpeechToText speechToText = new SpeechToText(iamOptions);
    speechToText.setEndPoint("https://gateway-syd.watsonplatform.net/speech-to-text/api");