Google Cloud语音识别权限被拒绝错误

时间:2018-11-24 12:48:21

标签: python google-cloud-platform google-speech-api

我正在尝试为基于python的个人助理执行Google云语音识别。我有一个服务帐户,还设置了GOOGLE_ACCOUNT_CREDENTIALS,还启用了API。但是它不起作用。这是官方页面上的示例代码。

from google.cloud import speech
client = speech.SpeechClient()
results = client.recognize(
    audio=speech.types.RecognitionAudio(
        uri='gs://my-bucket/recording.flac',
    ),
    config=speech.types.RecognitionConfig(
        encoding='LINEAR16',
        language_code='en-US',
        sample_rate_hertz=44100,
    ),
)
for result in results:
    for alternative in result.alternatives:
        print('=' * 20)
        print('transcript: ' + alternative.transcript)
        print('confidence: ' + str(alternative.confidence))

错误:

(base) C:\Users\mnauf>C:/Users/mnauf/Anaconda3/python.exe d:/Programming/python/mic.py
Traceback (most recent call last):
  File "C:\Users\mnauf\Anaconda3\lib\site-packages\google\api_core\grpc_helpers.py", line 59, in error_remapped_callable
    return callable_(*args, **kwargs)
  File "C:\Users\mnauf\Anaconda3\lib\site-packages\grpc\_channel.py", line 533, in __call__
    return _end_unary_response_blocking(state, call, False, None)
  File "C:\Users\mnauf\Anaconda3\lib\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 = "The caller does not have permission"
        debug_error_string = "{"created":"@1543062997.122000000","description":"Error received from peer","file":"src/core/lib/surface/call.cc","file_line":1017,"grpc_message":"The caller
does not have permission","grpc_status":7}"
>

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

Traceback (most recent call last):
  File "d:/Programming/python/mic.py", line 10, in <module>
    sample_rate_hertz=44100,
  File "C:\Users\mnauf\Anaconda3\lib\site-packages\google\cloud\speech_v1\gapic\speech_client.py", line 227, in recognize
    request, retry=retry, timeout=timeout, metadata=metadata)
  File "C:\Users\mnauf\Anaconda3\lib\site-packages\google\api_core\gapic_v1\method.py", line 139, in __call__
    return wrapped_func(*args, **kwargs)
  File "C:\Users\mnauf\Anaconda3\lib\site-packages\google\api_core\retry.py", line 260, in retry_wrapped_func
    on_error=on_error,
  File "C:\Users\mnauf\Anaconda3\lib\site-packages\google\api_core\retry.py", line 177, in retry_target
    return target()
  File "C:\Users\mnauf\Anaconda3\lib\site-packages\google\api_core\timeout.py", line 206, in func_with_timeout
    return func(*args, **kwargs)
  File "C:\Users\mnauf\Anaconda3\lib\site-packages\google\api_core\grpc_helpers.py", line 61, 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 The caller does not have permission

1 个答案:

答案 0 :(得分:1)

uri='gs://my-bucket/recording.flac',行中,使用您要转换的文件将路径更改为您自己的存储桶,因为自bucket names are unique在GCP中以来,该存储桶确实存在,因此您无权访问使用您的服务帐户密钥。