【问题标题】:Explicitly using service account credentials in code for speech recognition在代码中显式使用服务帐户凭据进行语音识别
【发布时间】:2019-02-13 15:23:35
【问题描述】:

声明这是我第一次使用 Google Cloud,所以这可能是一个菜鸟问题。

我想使用 Google Cloud Speech 库对音频文件上的文本进行一些语音。我还想通过提供私钥文件在代码中明确说明要使用的 Google Cloud 服务帐户凭据。我该怎么做?

看来我想要的是this quickstart for speech recognitionthis example of how to set credentials within the code 的混合(explicit() 部分)。

我试过这样做,但是explicit() 使用google.cloud.storage 来设置客户端,

from google.cloud import storage
storage_client = storage.Client.from_service_account_json('service_account.json')

为了发出 API 请求。

设置

client = storage.Client.from_service_account_json('service_account.json')

然后运行

client.recognize(config, audio)

显然会抛出一个错误,指出client 没有该属性。我的猜测是我需要类似的东西,但是对于google.cloud.speech?我试过查看文档 - 我错过了什么吗?

【问题讨论】:

    标签: python google-cloud-platform


    【解决方案1】:

    几乎所有的 Google 客户端库都遵循相同的凭据设计模式。在下面的示例中,代码会加载凭据,然后使用这些凭据创建客户端。

    Link to the Speech Client

    from google.oauth2 import service_account
    from google.cloud import speech_v1
    from google.cloud.speech_v1 import enums
    
    SCOPES = ["https://www.googleapis.com/auth/cloud-platform"]
    SERVICE_ACCOUNT_FILE = 'service-account.json'
    
    cred = service_account.Credentials.from_service_account_file(
            SERVICE_ACCOUNT_FILE, scopes=SCOPES)
    
    client = speech_v1.SpeechClient(credentials=cred)
    

    OR(注意不指定通常可以的范围)

    client = speech_v1.SpeechClient.from_service_account_file(SERVICE_ACCOUNT_FILE)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-27
      • 2019-05-05
      • 2018-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多