首页 > 解决方案 > IBM Watson:未经授权

问题描述

我正在使用 Java 做一个包含文本到语音的应用程序。我使用了 IBM Watson Text-to-Speech API,但我的代码总是出现未经授权的错误。谁能帮我修复它?非常感谢!

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);

在我输入 API 密钥并安装应用程序后,我总是得到这样的日志:

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)

出错的行是

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

标签: javaandroidandroid-studioibm-watson

解决方案


这将是因为您使用了错误的密钥或错误的端点。服务的端点因地区而异,但您可以在为服务创建一组凭据时确定要使用的密钥和端点。如果您查看您的服务凭证,您将获得一个 iam 密钥和端点。


推荐阅读