【问题标题】:Speech-to-text: google.api_core.exceptions.PermissionDenied: 403语音转文本:google.api_core.exceptions.PermissionDenied:403
【发布时间】:2019-08-08 08:52:35
【问题描述】:

根据https://googleapis.github.io/google-cloud-python/latest/speech/index.html,我正在尝试使用 Google 语音转文本服务 我创建了项目,将音频上传到 gs: cloud,添加了权限,下载了名为 My First Project-7bb85a480131.json 的 json 文件。 https://console.cloud.google.com/storage/browser/mybucket?project=my-project

这是我的文件:

import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="/home/joo/Документы/LocalRepository/robotze/My First Project-7bb85a480131.json"
from google.cloud import speech
client = speech.SpeechClient()
audio = speech.types.RecognitionAudio(
uri='gs://zaudio/audio.mp3')
config = speech.types.RecognitionConfig(
encoding=speech.enums.RecognitionConfig.AudioEncoding.LINEAR16,
language_code='ru-RU',
sample_rate_hertz=44100)
operation = client.long_running_recognize(config=config, audio=audio)
op_result = operation.result()
for result in op_result.results:
    for alternative in result.alternatives:
        print('=' * 20)
        print(alternative.transcript)
        print(alternative.confidence)

问题:我知道了

google.api_core.exceptions.PermissionDenied: 403 my-service-account@my-project.iam.gserviceaccount.com 没有 storage.objects.get 访问 mybucket/audio.mp3。

完整的回溯

/home/joo/anaconda3/bin/python /home/joo/Документы/LocalRepository/robotze/speech-to-text-googlecloud.py
Traceback (most recent call last):
  File "/home/joo/anaconda3/lib/python3.6/site-packages/google/api_core/grpc_helpers.py", line 57, in error_remapped_callable
    return callable_(*args, **kwargs)
  File "/home/joo/anaconda3/lib/python3.6/site-packages/grpc/_channel.py", line 565, in __call__
    return _end_unary_response_blocking(state, call, False, None)
  File "/home/joo/anaconda3/lib/python3.6/site-packages/grpc/_channel.py", line 467, in _end_unary_response_blocking
    raise _Rendezvous(state, None, None, deadline)
grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with:
    status = StatusCode.PERMISSION_DENIED
    details = "my-service-account@my-project.iam.gserviceaccount.com does not have storage.objects.get access to mybucket/audio.mp3."
    debug_error_string = "{"created":"@1565253582.126380437","description":"Error received from peer ipv4:74.125.131.95:443","file":"src/core/lib/surface/call.cc","file_line":1052,"grpc_message":"my-service-account@my-project.iam.gserviceaccount.com does not have storage.objects.get access to mybucket/audio.mp3.","grpc_status":7}"
>

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/joo/Документы/LocalRepository/robotze/speech-to-text-googlecloud.py", line 46, in <module>
    operation = client.long_running_recognize(config=config, audio=audio)
  File "/home/joo/anaconda3/lib/python3.6/site-packages/google/cloud/speech_v1/gapic/speech_client.py", line 341, in long_running_recognize
    request, retry=retry, timeout=timeout, metadata=metadata
  File "/home/joo/anaconda3/lib/python3.6/site-packages/google/api_core/gapic_v1/method.py", line 143, in __call__
    return wrapped_func(*args, **kwargs)
  File "/home/joo/anaconda3/lib/python3.6/site-packages/google/api_core/retry.py", line 273, in retry_wrapped_func
    on_error=on_error,
  File "/home/joo/anaconda3/lib/python3.6/site-packages/google/api_core/retry.py", line 182, in retry_target
    return target()
  File "/home/joo/anaconda3/lib/python3.6/site-packages/google/api_core/timeout.py", line 214, in func_with_timeout
    return func(*args, **kwargs)
  File "/home/joo/anaconda3/lib/python3.6/site-packages/google/api_core/grpc_helpers.py", line 59, in error_remapped_callable
    six.raise_from(exceptions.from_grpc_error(exc), exc)
  File "<string>", line 3, in raise_from
google.api_core.exceptions.PermissionDenied: 403 my-service-account@my-project.iam.gserviceaccount.com does not have storage.objects.get access to mybucket/audio.mp3.

Process finished with exit code 1

我尝试了什么:gcloud auth application-default login - 在浏览器中登录有效,但仍然出现 403 错误

【问题讨论】:

    标签: python speech-recognition gcloud speech-to-text


    【解决方案1】:

    根据我在您的日志中看到的内容,您可以在代码中验证您的服务帐户(您当前正在验证:starting-account-*******-239919.iam.gserviceaccount.com) ,但是,该服务帐户没有对象“zaudio/audio.mp3”的“storage.objects.get”权限。

    所以你可以:

    A.- 为该服务帐户授予适当的权限(可能是该存储桶内的角色“storage.objectViewer”就足够了,但您也可以使用角色“storage.admin”对其进行设置,这样它就可以拥有更多控制该存储桶和其他存储桶)。

    B.- 使用具有适当权限的其他服务帐户进行身份验证。

    【讨论】:

    • 嗯,不小心我重新创建了新项目并将文件重新上传到新存储桶,代码以 0 代码完成,所以权限解决了,也许我第一次错过了。但它不返回结果,但音频只有 7mb,但这是另一个问题
    • 很高兴知道它已解决,如果您遇到未来的问题(有其他错误),您可以随时创建另一个问题。此外,请确保您的对象存在并在每个步骤之间使用打印,以确保您正确创建了在每个操作中发送的对象。
    【解决方案2】:

    我解决了以下问题:

    “google.api_core.exceptions.PermissionDenied: 403 my-service-account@my-project.iam.gserviceaccount.com does not have storage.objects.get access to mybucket/audio.mp3.” 
    
    

    要解决此问题:转到您的存储桶,单击三个点,选择“编辑权限”,实体集“用户”,名称设置您的电子邮件(在本例中为 my-service-account@my-project.iam .gserviceaccount.com),访问:“读者”。保存并重试。这应该可以解决这个问题。无论您是否创建了存储桶,诸如此类,您都必须执行此步骤以显式设置权限。希望这很有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-29
      • 2014-07-02
      • 2019-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-22
      相关资源
      最近更新 更多