如果我只有操作名称,如何在 python 中获得云语音 api 响应?

How to get cloud speech api response in python if I only have the operation name?

我正在发送用于语音识别的长语音识别文件。

operation = client.long_running_recognize(config, audio)
operation_name = operation._operation.name

还有另一个文件,我必须在其中使用 operation_name(由 google 语音 API 返回)再次取回响应。

参考:

我尝试了 "Long-Running Operations Client" 的 get_operation 方法:

from google.api_core import operations_v1
api = operations_v1.OperationsClient()
name = ...
response = api.get_operation(name)

但是我收到以下行的错误:

api = operations_v1.OperationsClient()

TypeError: init() missing 1 required positional argument: 'channel'

得到 Github 个问题的答案:

from google.cloud import speech
client = speech.SpeechClient()

from google.api_core import operations_v1
api = operations_v1.OperationsClient(client.transport.channel)
name = ...
response = api.get_operation(name)

参考:Github