Javascript 从谷歌云函数检查谷歌语音API操作

Javascript 从谷歌云函数检查谷歌语音API操作,javascript,node.js,firebase,google-cloud-functions,google-cloud-speech,Javascript,Node.js,Firebase,Google Cloud Functions,Google Cloud Speech,我正在使用谷歌语音API转录长文件。该API是从Google云函数调用的我想稍后用检查LongRunningRecognite的结果。我知道操作的名称/id,但是我找不到一个好方法来通过操作名称从谷歌云功能检查操作状态 当然,我可以对该url发出GET HTTP请求: https://speech.googleapis.com/v1/operations/{name}?key=API_KEY 这是一个有效的示例代码: const functions = require('firebase-f

我正在使用谷歌语音API转录长文件。该API是从Google云函数调用的我想稍后用检查
LongRunningRecognite
的结果。我知道操作的
名称/id
,但是我找不到一个好方法来通过操作名称从谷歌云功能检查操作状态

当然,我可以对该url发出GET HTTP请求:

https://speech.googleapis.com/v1/operations/{name}?key=API_KEY 
这是一个有效的示例代码:

const functions = require('firebase-functions');
const speech = require('@google-cloud/speech');
const request = require('request');

exports.transcribe = functions.storage.object().onFinalize((object) => {
  // some code to get data required for speech API
  const payload = {
    audio: {
      uri: 'some_uri/to/google/storage/file'
    },
    config: {
      encoding: 'FLAC',
      languageCode: 'en-US'
    }
  };

  const client = new speech.SpeechClient({
    projectId: 'my-project-id'
  });

  client.longRunningRecognize(payload)
    .then(responses => {
      const operation = responses[0];
      // current example of getting operation status by operation name with HTTP call
      request(`https://speech.googleapis.com/v1/operations/${operation.latestResponse.name}?key=MY-API-KEY`, (error, response, body) => {
        console.log('Operation status response: ', body);
      });
    });
});
但似乎应该有一个更明确的方法来做到这一点。至少 我可以找到这个和这个的描述,所以我想要这样的东西来检查状态:

// this line is the most confusing part of the puzzle
const client = longrunning.operationsClient();
const name = '';
client.getOperation({name: name}).then(function(responses) {
  var response = responses[0];
  // doThingsWith(response)
});

谢谢你的帮助

我怀疑你已经离开了,但为了别人,我会回答的

当您运行
longRunningRecognize
方法时,SDK开始轮询longrunning
操作。为您获取
。只需使用上的
.on设置节点事件侦听器

操作
对象(从LongRunningRecognite返回的承诺的第一个数组元素)在
进度
完成
错误
上发出节点事件

OP代码的更新:

client.longRunningRecognize(payload)
  .then(responses => {
    const operation = responses[0];
    operation.on('progress', (metadata, apiResponse) => {
      console.log(JSON.stringify(metadata))
    });
  });
示例输出:(与
https://speech.googleapis.com/v1/operations/...

请注意,没有明显的方法通过异步操作获取部分转录的文本,只有状态百分比。

以下是您要查找的代码,用于检查带有操作名称的
LongRunningRecognite
的状态:

const client=newspeech.SpeechClient();
常量操作名='…';
client.checkLongRunningRecognitizeProgress(operationName)。然后(res=>{
如果(已完成){
var response=res.result.responses[0];
//doThingsWith(回应)
}
});
{"startTime":{"seconds":"1529629181","nanos":790333000},"lastUpdateTime":{"seconds":"1529629182","nanos":661910000}}
{"progressPercent":26,"startTime":{"seconds":"1529629181","nanos":790333000},"lastUpdateTime":{"seconds":"1529629245","nanos":48465000}}
{"progressPercent":52,"startTime":{"seconds":"1529629181","nanos":790333000},"lastUpdateTime":{"seconds":"1529629307","nanos":516891000}}
{"progressPercent":78,"startTime":{"seconds":"1529629181","nanos":790333000},"lastUpdateTime":{"seconds":"1529629369","nanos":680341000}}