python - 谷歌云合成语音501方法未找到

标签 python google-api google-cloud-platform gcloud google-text-to-speech

严格使用作为示例提供的代码来运行谷歌云文本到语音 API:

def synthesize_text(text):
    """Synthesizes speech from the input string of text."""
    from google.cloud import texttospeech
    client = texttospeech.TextToSpeechClient()

    input_text = texttospeech.types.SynthesisInput(text=text)

    # Note: the voice can also be specified by name.
    # Names of voices can be retrieved with client.list_voices().
    voice = texttospeech.types.VoiceSelectionParams(
        language_code='en-US',
        ssml_gender=texttospeech.enums.SsmlVoiceGender.FEMALE)

    audio_config = texttospeech.types.AudioConfig(
        audio_encoding=texttospeech.enums.AudioEncoding.MP3)

    response = client.synthesize_speech(input_text, voice, audio_config)

    # The response's audio_content is binary.
    with open('output.mp3', 'wb') as out:
        out.write(response.audio_content)
        print('Audio content written to file "output.mp3"')

我收到上述错误消息 google.api_core.exceptions.MethodNotImplemented 501 未找到方法

这似乎是 Google 内部错误。我已经仔细检查过我的凭据。 错误来自此特定行:response = client.synthesize_speech(input_text, voice, audio_config)

请帮忙。

最佳答案

此时,由于某种原因,python 客户端库有 code for v1 and v1beta1 API versions 。但是,检查 the dicovery service for Google APIs ,我们可以看到唯一可用的端点适用于 v1beta1。

如果您关注this library docs并使用 from google.cloud import texttospeech_v1,您的客户端将尝试使用 https://texttospeech.googleapis.com/v1/ 而不是 https://texttospeech.googleapis.com/v1beta1/。事实上,错误不是 4XX 而是 5XX,这让我认为端点本身可能存在,但它们尚未实现,或者不允许您发现它们(因此它们看起来没有实现)。

尝试将您的导入更改为 v1beta1 doc 中的导入(from google.cloud import texttospeech_v1beta1),看看这是否有任何区别。

关于python - 谷歌云合成语音501方法未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50932363/

相关文章:

google-api - Google token 刷新返回 "Token has been expired or revoked."

google-api - 刷新 G Suite Admin SDK 目录 API 的 OAuth2 token 时出错

python - 字典查询Python?

python - 在没有 multipart/form-data 的 Flask 中 ReSTfully 上传文件

python - numpy.where() 具有 3 个或更多条件

bash - 如何为 gcloud 获取 shell 命令完成(自动完成)?

google-cloud-platform - 允许未经身份验证的参数不起作用

python - 运行时范围和 `main` 符号在函数内部或外部不同

image - Google AJAX API - 我如何获得超过 4 个结果?

python - Google Cloud DataFlow 作业在几个小时后发出警报