IBM-Watson文字语音转换引发“ 403:禁止访问”错误

时间:2020-03-22 23:41:38

标签: python text-to-speech ibm-watson speech-to-text

我正在尝试使用IBM watson api进行文本到语音服务。如果我使用curl命令,该服务将起作用,但是当我尝试使用Python SDK使用srrvice时,它将使我陷入错误。

回溯(最近通话最近): 发送中的文件“ /anaconda3/lib/python3.6/site-packages/ibm_cloud_sdk_core/base_service.py”,第234行 response.status_code,error_message,http_response = response) ibm_cloud_sdk_core.api_exception.ApiException:错误:禁止,代码:403 方法失败,状态码为403:禁止

下面是curl命令

curl -X GET -u "apikey:myapiKey" --output hello_world.wav "https://api.eu-de.text-to-speech.watson.cloud.ibm.com/text-to-speech/api/v1/synthesize?accept=audio/wav&text=Hallo%20Welt&voice=de-DE_DieterVoice"

下面是python代码

from ibm_watson import TextToSpeechV1
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator


from ibm_watson import ApiException
import json

IBM_API_ENDPOINT = "https://api.eu-de.text-to-speech.watson.cloud.ibm.com/text-to-speech/api/v1/synthesize"


IBM_TTS_API_KEY = "myAPIKey"

authenticator = IAMAuthenticator(IBM_TTS_API_KEY)
text_to_speech = TextToSpeechV1(authenticator=authenticator)
text_to_speech.set_service_url(IBM_API_ENDPOINT)


try:
    with open('IBM.wav', 'wb') as audio_file:
        audio_file.write(text_to_speech.synthesize("Hallo world", voice='de-DE_DieterVoice', accept='audio/wav').get_result().content)

except ApiException as ex:
    print("Method failed with status code " + str(ex.code) + ": " + ex.message)

2 个答案:

答案 0 :(得分:1)

这是因为您要在端点末尾添加方法和版本。在使用适用于IBM-Watson的Python SDK时,SDK会在调用synthesize时应用该方法。因此,您的终结点应该仅是:

IBM_API_ENDPOINT = "https://api.eu-de.text-to-speech.watson.cloud.ibm.com"

查看API文档以获取更多详细信息-https://cloud.ibm.com/apidocs/text-to-speech/text-to-speech?code=python#service-endpoint

答案 1 :(得分:1)

检查当前使用量(10000个字符)。当我达到每月最大字符数时,我遇到了同样的问题。

(千字符:使用10000的21091)