Python 3.x 如何将API密钥凭证与Google文本到语音客户端库方法一起使用?
我想用一个API密钥实例化一个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
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))
我收到了一个带有文本内容的回复。如果我要这样做,我如何将收到的内容保存为音频文件?您找到解决方案了吗?还没有!我想听听你的意见。