Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何将API密钥凭证与Google文本到语音客户端库方法一起使用?_Python 3.x_Audio_Google Authentication_Google Text To Speech - Fatal编程技术网

Python 3.x 如何将API密钥凭证与Google文本到语音客户端库方法一起使用?

Python 3.x 如何将API密钥凭证与Google文本到语音客户端库方法一起使用?,python-3.x,audio,google-authentication,google-text-to-speech,Python 3.x,Audio,Google Authentication,Google Text To Speech,我想用一个API密钥实例化一个TextToSpeechClient(),而不是全局GOOGLE\u应用程序\u凭据变量中提供的服务帐户凭据。具体来说,我想使用相关网站上提供的以下功能,但由于缺少凭据,我无法实例化客户端 def synthesize_text(text): """Synthesizes speech from the input string of text.""" from google.cloud import texttospeech client

我想用一个API密钥实例化一个
TextToSpeechClient()
,而不是全局
GOOGLE\u应用程序\u凭据
变量中提供的服务帐户凭据。具体来说,我想使用相关网站上提供的以下功能,但由于缺少凭据,我无法实例化
客户端

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',
        name='en-US-Standard-C',
        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文本到语音网站上的JSON和
请求
包时,我克服了凭证问题,如下所示:

r = requests.post('https://texttospeech.googleapis.com/v1beta1/text:synthesize?key=API_KEY', data = json.dumps(data))

我收到了一个带有文本内容的回复。如果我要这样做,我如何将收到的内容保存为音频文件?

您找到解决方案了吗?还没有!我想听听你的意见。