【问题标题】:How to add credentials to Google text to speech API?如何将凭据添加到 Google 文本到语音 API?
【发布时间】:2018-05-21 09:18:43
【问题描述】:

我是 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"')

这是错误,

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.

我已经有凭据 JSON 文件,但我无法配置代码来验证我的请求。 请帮忙!

【问题讨论】:

    标签: python-3.x google-api google-authentication google-text-to-speech


    【解决方案1】:

    你可以试试这个代码:

    from google.oauth2 import service_account
    
    credentials = service_account.Credentials.from_service_account_file('yourkey.json')
    
    client = texttospeech.TextToSpeechClient(credentials=credentials)
    

    【讨论】:

      【解决方案2】:

      有两种方式:

      1 方式: 如果您使用 Json 文件,那么最好将 json 路径设置为环境变量,如果您这样做,那么您无需在编码中设置它会自动从那里获得您的许可证

      GOOGLE_APPLICATION_CREDENTIALS=[path]
      

      两种方式:

      我有我不了解 python 的 Java 代码,所以你可以从这里得到想法:

      String jsonPath = "file.json"; 
       CredentialsProvider credentialsProvider = FixedCredentialsProvider.create(ServiceAccountCredentials.fromStream(new FileInputStream(jsonPath)));
      TextToSpeechSettings settings = TextToSpeechSettings.newBuilder().setCredentialsProvider(credentialsProvider).build();
          Instantiates a client
      TextToSpeechClient textToSpeechClient = TextToSpeechClient.create(settings)
      

      【讨论】:

        【解决方案3】:

        您可以通过不同的方式验证您的 Google 凭据。 一种是通过设置操作系统环境,另一种是在您发起请求时进行身份验证。

        我建议使用oauth2client 库供 python 进行身份验证。 除此之外,请参考我在 Github (Link) 上的示例。

        【讨论】:

          【解决方案4】:

          这似乎是一个古老的讨论,但我想发表评论,也许有人会遇到像我这样的情况:)) 对于 nodejs 客户端,我设法通过这种方式对其进行身份验证:

          const client = new textToSpeech.TextToSpeechClient({
          credentials: {
          private_key: "??",
          client_email: "???",
           }
          });
          

          【讨论】:

            猜你喜欢
            • 2018-10-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-03-03
            • 2017-10-10
            • 2017-12-17
            相关资源
            最近更新 更多