小编cmb*_*b28的帖子

如何将凭据添加到 Google Text to Speech API?

我是 Python 新手。我想使用 Google 文本到语音转换 API,因为我在下面的代码中使用了它,但由于错误,我无法访问该 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"')
Run Code Online (Sandbox Code Playgroud)

这是错误,

google.auth.exceptions.DefaultCredentialsError: …
Run Code Online (Sandbox Code Playgroud)

google-api google-authentication python-3.x google-text-to-speech

1
推荐指数
1
解决办法
3527
查看次数