【问题标题】:Authentication to Google Cloud Python API Library stopped working对 Google Cloud Python API 库的身份验证停止工作
【发布时间】:2017-03-13 12:14:13
【问题描述】:

我在 Google Cloud API 的 Python 库中的身份验证存在问题。 起初它工作了几天没有问题,但突然 API 调用没有显示在 Google CloudPlatform 的 API 概述中。

我创建了一个服务帐户并将json 文件存储在本地。然后我将环境变量GCLOUD_PROJECT设置为项目ID,GOOGLE_APPLICATION_CREDENTIALS设置为json文件的路径。

from google.cloud import speech
client = speech.Client()
print(client._credentials.service_account_email)

打印正确的服务帐户电子邮件。

以下代码成功转录 audio_file,但我的 Google Cloud 项目的仪表板未显示激活的语音 API 图表的任何内容。

import io
with io.open(audio_file, 'rb') as f:
    audio = client.sample(f.read(), source_uri=None, sample_rate=48000, encoding=speech.encoding.Encoding.FLAC)

alternatives = audio.sync_recognize(language_code='de-DE')

在某些时候,代码也出现了一些关于使用限制的错误。我猜由于身份验证不成功,以某种方式使用了免费/受限选项。

我还尝试了通过安装 Google Cloud SDK 和gcloud auth application-default login 进行身份验证的替代选项,但没有成功。

我不知道从哪里开始解决问题。 任何帮助表示赞赏!

(我的系统运行的是带有 Anaconda 的 Windows 7)

编辑: 错误计数 (Fehler) 随着对 API 的调用而增加。如何获取有关错误的详细信息?!

【问题讨论】:

    标签: python google-cloud-vision google-cloud-speech google-cloud-python gcloud-python


    【解决方案1】:

    确保在设置GOOGLE_APPLICATION_CREDENTIALS 环境变量时使用的是绝对路径。此外,您可能想要try inspecting the access token using OAuth2 tokeninfo 并确保它的回复中有"scope": "https://www.googleapis.com/auth/cloud-platform"

    如果你initialize the client with GRPC enabled:有时你会得到不同的错误信息:

    0.24.0: speech_client = speech.Client(_use_grpc=True)

    0.23.0: speech_client = speech.Client(use_gax=True)

    通常这是一个编码问题,您可以尝试使用示例音频或尝试使用 Unix rec 工具之类的工具生成 LINEAR16 示例:

    rec --channels=1 --bits=16 --rate=44100 audio.wav trim 0 5
    

    ...

    with io.open(speech_file, 'rb') as audio_file:
        content = audio_file.read()
        audio_sample = speech_client.sample(
            content,
            source_uri=None,
            encoding='LINEAR16',
            sample_rate=44100)
    

    其他说明:

    • 同步识别仅限于 60 秒的音频,您必须使用异步才能获得更长的音频
    • 如果您还没有,请为您的帐户设置结算方式

    【讨论】:

    • 它现在正在工作。首先,我将GOOGLE_APPLICATION_CREDENTIALS 设置为绝对路径,这并没有改变任何东西。其次,我将包google-cloud-speech 更新为版本0.24。之后,API 调用将显示在仪表板上。
    【解决方案2】:

    关于使用问题,问题实际上是当你使用新的 google-cloud 库访问 ML API 时,似乎每个人都对每个人共享的项目进行身份验证(因此它说你已经用完了你的限制,即使你没有使用任何东西)。要检查并确认这一点,您可以使用 python 客户端库调用您尚未启用的 ML API,即使它不应该为您提供结果。这个问题对其他语言客户端库和操作系统仍然存在,所以我怀疑这是他们的 grpc 的问题。

    因此,为了确保一致性,我总是使用使用我的 API 密钥的旧 googleapiclient。以下是使用翻译 API 的示例:

    from googleapiclient import discovery
    
    service = discovery.build('translate', 'v2', developerKey='')
    
    service_request = service.translations().list(q='hello world', target='zh')
    result = service_request.execute()
    
    print(result)

    对于语音 API,大致如下:

    from googleapiclient import discovery
    
    service = discovery.build('speech', 'v1beta1', developerKey='')
    
    service_request = service.speech().syncrecognize()
    result = service_request.execute()
    
    print(result)

    您可以在https://developers.google.com/api-client-library/python/apis/ 获取发现 API 列表,其中的语音位于https://developers.google.com/resources/api-libraries/documentation/speech/v1beta1/python/latest/

    使用发现库的其他好处之一是,与当前库相比,您可以获得更多选择,尽管通常实施起来有点麻烦。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-28
      • 2017-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-17
      相关资源
      最近更新 更多