Lazy
Lazy

Reputation: 11

Watson Speech to Text - status: 401, error: Unauthorized

I am trying to integrate the speech to text service in my chatbot but receiving the error metioned in the title. I just used the service like described in the documentation. They changed the authorisation method from username + password to api key right? This is the error I get:

Feb 26, 2019 11:18:38 AM okhttp3.internal.platform.Platform log
INFO: --> POST https://stream.watsonplatform.net/speech-to-text/api/v1/recognize http/1.1 (-1-byte body)
Feb 26, 2019 11:18:40 AM okhttp3.internal.platform.Platform log
INFO: <-- 401 Unauthorized https://stream.watsonplatform.net/speech-to-text/api/v1/recognize (2434ms, 37-byte body)
Feb 26, 2019 11:18:40 AM com.ibm.watson.developer_cloud.service.WatsonService processServiceCall
SEVERE: POST https://stream.watsonplatform.net/speech-to-text/api/v1/recognize, status: 401, error: Unauthorized
Exception in thread "AWT-EventQueue-0" com.ibm.watson.developer_cloud.service.exception.UnauthorizedException: Unauthorized: Access is denied due to invalid credentials. Tip: Did you set the Endpoint?
    at com.ibm.watson.developer_cloud.service.WatsonService.processServiceCall(WatsonService.java:368)
    at com.ibm.watson.developer_cloud.service.WatsonService$WatsonServiceCall.execute(WatsonService.java:429)
    at STTService.erkenneAudio(STTService.java:66)
    at GUI$recordAudioButtonListener.actionPerformed(GUI.java:179)

Nevertheless, this is what my code looks like:

    SpeechToText stt = new SpeechToText();

    stt.setApiKey("3PHwb6P36A0hgiovasdcqgesVyDZEO7awg2L1be");
    stt.setEndPoint("https://stream.watsonplatform.net/speech-to-text/api");

    try {
        RecognizeOptions recognizeOptions = new RecognizeOptions.Builder()
                  .audio(new File("C:/Users/jvermaet/Desktop/hallo.wav"))
                  .contentType(HttpMediaType.AUDIO_WAV)
                  .build();

        SpeechRecognitionResults srr = stt.recognize(recognizeOptions).execute();
        System.out.println(srr.toString());
                    
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        System.out.println("OO");
    }

Upvotes: 0

Views: 398

Answers (1)

Lazy
Lazy

Reputation: 11

I just needed to use the IAmOptions Contructor. If you cannot import the IAmOptions it might be because you use an old version of the sdk, which was my main problem.

IamOptions options = new IamOptions.Builder()
.apiKey("{apikey}")
.build();
Assistant assistant = new Assistant("{version}", options);
assistant.setEndPoint("{url}");

Upvotes: 1

Related Questions