简体   繁体   中英

IBM Watson: Unauthorized

I am doing an app which includes text-to-speech, using Java. I used IBM Watson Text-to-Speech API but I always have Unauthorized Error with my code. Could anyone help me to fix it? Thank you very much!

IamOptions options = new IamOptions.Builder()
                        .apiKey({apikey})
                        .build();
                TextToSpeech textToSpeech = new TextToSpeech(options);
                textToSpeech.setEndPoint("https://stream.watsonplatform.net/text-to-speech/api");
                  try {
                      SynthesizeOptions synthesizeOptions =
                              new SynthesizeOptions.Builder()
                                      .text(name)
                                      .accept("audio/wav")
                                      .voice("en-US_AllisonVoice")
                                      .build();

                      InputStream inputStream =
                              textToSpeech.synthesize(synthesizeOptions).execute().getResult();
                      InputStream in = WaveUtils.reWriteWaveHeader(inputStream);

After I key in the API key and install the app, I always get such log:

AndroidRuntime: FATAL EXCEPTION: inference
    Process: org.tensorflow.demo, PID: 21282
    com.ibm.cloud.sdk.core.service.exception.UnauthorizedException: Unauthorized
        at com.ibm.cloud.sdk.core.service.BaseService.processServiceCall(BaseService.java:478)
        at com.ibm.cloud.sdk.core.service.BaseService$WatsonServiceCall.execute(BaseService.java:544)
        at org.tensorflow.demo.DetectorActivity$3.run(DetectorActivity.java:351)
        at android.os.Handler.handleCallback(Handler.java:907)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:216)
        at android.os.HandlerThread.run(HandlerThread.java:65)

The line which went wrong is

InputStream inputStream =
                              textToSpeech.synthesize(synthesizeOptions).execute().getResult();

This will be because you are using either the wrong key or the wrong endpoint. Endpoints for the service vary across regions, but you can determine which key and endpoint to use when you create a set of credentials for the service. If you take a look at your service credentials you will be given an iam key and the endpoint.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM