google 文本到语音 api 无法找到 json 键的路径,使用 python 在 windows 上使用 anaconda

google text to speech api unable to find path of json key using anaconda with python on windows

我正在尝试使用 google 云 api 来使用文本到语音。为此,它需要访问我保存到

的 json 密钥
C:\keys\*****.json

并将路径设置为

set GOOGLE_APPLICATION_CREDENTIALS="C:\keys\*****.json"

当我 运行 示例脚本时,我收到错误消息,我无法在该路径上找到文件。

C:\Users\dsandhu\Documents\linux\googlecloud\python-docs-samples\texttospeech\testing>python sampletest.py
Traceback (most recent call last):
  File "sampletest.py", line 28, in <module>
    synthesize_text("hello world")
  File "sampletest.py", line 4, in synthesize_text
    client = texttospeech.TextToSpeechClient()
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\google\cloud\texttospeech_v1\gapic\text_to_speech_client.py", line 84, in __init__
    scopes=self._DEFAULT_SCOPES,
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\google\api_core\grpc_helpers.py", line 170, in create_channel
    credentials, _ = google.auth.default(scopes=scopes)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\google\auth\_default.py", line 294, in default
    credentials, project_id = checker()
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\google\auth\_default.py", line 165, in _get_explicit_environ_credentials
    os.environ[environment_vars.CREDENTIALS])
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\google\auth\_default.py", line 89, in _load_credentials_from_file
    'File {} was not found.'.format(filename))
google.auth.exceptions.DefaultCredentialsError: File "C:\keys\*****.json" was not found.

我正在使用 anaconda(作为管理员)和 python 在 anaconda 终端中设置变量。我也尝试在 windows cmd 中设置它(使用相同的命令设置)但没有成功。

如果你想测试它,我也会张贴凭证:

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

    #text = "Hello world"
    input_text = texttospeech.types.SynthesisInput(text=text)
    #input_text = texttospeech.types.SynthesisInput(text="Hellow world")

    # 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"')



synthesize_text("hello world")

使用此 link

我只需要删除引号并执行

set GOOGLE_APPLICATION_CREDENTIALS=C:\keys\******.json

它奏效了。