语音转文本:google.api_core.exceptions.PermissionDenied:403

Speech-to-text: google.api_core.exceptions.PermissionDenied: 403

根据 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 错误

根据我在您的日志中看到的内容,您可以在您的代码中验证您的服务帐户(您当前正在使用以下方式进行身份验证:starting-account-*******-239919.iam.gserviceaccount.com) ,但是,该服务帐户没有对对象 "zaudio/audio.mp3".

的 "storage.objects.get" 权限

因此您可以:

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

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

我解决了以下问题:

“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),访问:“Reader”。保存并重试。这应该可以解决此问题。不管你有没有创建过bucket等等,你都必须做这一步来显式设置权限。希望这有用。