cmb*_*b28 1 google-api google-authentication python-3.x google-text-to-speech
我是 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: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or
explicitly create credential and re-run the application. For more
information, please see
https://developers.google.com/accounts/docs/application-default-credentials.
Run Code Online (Sandbox Code Playgroud)
我已经有凭证 JSON 文件,但我无法配置代码来验证我的请求。请帮忙!
小智 5
你可以试试这个代码:
from google.oauth2 import service_account
credentials = service_account.Credentials.from_service_account_file('yourkey.json')
client = texttospeech.TextToSpeechClient(credentials=credentials)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3527 次 |
| 最近记录: |