简体   繁体   English

如何从 Google Apps 脚本授权 Google Speech-to-text?

[英]How can I authorize Google Speech-to-text from Google Apps script?

I'm trying to execute google-speech-to-text from apps script.我正在尝试从应用程序脚本执行 google-speech-to-text。 Unfortunately, I cannot find any examples for apps script or pure HTTP, so I can run it using simple UrlFetchApp.不幸的是,我找不到任何应用程序脚本或纯 HTTP 的示例,所以我可以使用简单的 UrlFetchApp 运行它。

I created a service account and setup a project with enabled speech-to-text api, and was able to successfully run recognition using command-line example我创建了一个服务帐户并设置了一个启用语音到文本 api 的项目,并且能够使用命令行示例成功运行识别

curl -s -H "Content-Type: application/json" \ -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \ https://speech.googleapis.com/v1/speech:recognize \ -d @sync-request.json curl -s -H "Content-Type: application/json" \ -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \ https://speech.googleapis.com/v1/speech:识别\ -d @sync-request.json

which I can easily translate to UrlFetchApp call, but I don't have an idea to generate access token created by我可以轻松地将其转换为 UrlFetchApp 调用,但我不知道生成由

gcloud auth application-default print-access-token gcloud auth 应用程序默认打印访问令牌

Is there a way to get it from apps script using service account credentials?有没有办法使用服务帐户凭据从应用程序脚本中获取它?

Or is there any other way to auth and access speech-to-text from apps script?或者有没有其他方法可以从应用程序脚本验证和访问语音到文本?

The equivalent of retrieving access tokens through service accounts is through the apps script oauth library .通过服务帐户检索访问令牌的等效方法是通过应用程序脚本 oauth library The library handles creation of the JWT token.该库处理 JWT 令牌的创建。

Sample here样品在这里

Using the answer from TheMaster, I was able to build a getToken solution for my case使用 TheMaster 的答案,我能够为我的案例构建一个 getToken 解决方案

` `

function check() {
  var service = getService();
  if (service.hasAccess()) {
    Logger.log(service.getAccessToken());
  } else {
    Logger.log(service.getLastError());
  }
}

function getService() {
  return OAuth2.createService('Speech-To-Text Token')
      .setTokenUrl('https://oauth2.googleapis.com/token')
      .setPrivateKey(PRIVATE_KEY)
      .setIssuer(CLIENT_EMAIL)
      .setPropertyStore(PropertiesService.getScriptProperties())
      .setScope('https://www.googleapis.com/auth/cloud-platform');
}

` `

The code for transcribe itself, is转录本身的代码是

function transcribe(){
  var payload = {
    "config": {
      "encoding" : "ENCODING_UNSPECIFIED",
      "sampleRateHertz": 48000,
      "languageCode": "en-US",
      "enableWordTimeOffsets": false
    },
    "audio": {
      content: CONTENT
    }
  };

  var response = UrlFetchApp.fetch(
    "https://speech.googleapis.com/v1/speech:recognize", {
      method: "GET",
      headers: {
        "Authorization" : "Bearer " + getService().getAccessToken()
      },
      contentType: "application/json",
      payload: JSON.stringify(payload),
      muteHttpExceptions: true
    });  

  Logger.log(response.getContentText());

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更改基于 Google Apps 脚本的 Google Workspace 附加组件中的“授权访问”按钮 - How can I change the `AUTHORIZE ACCESS` button in a Google Apps Script based Google Workspace Add-On 如何使用Google Apps脚本在电子表格的单元格中剪切文本? - How can I clip text in in cell in spreadsheet with google apps script? 我可以使用 Google Apps Script 使 Google 表单显示 Google 表格中的随机文本吗? - Can I use Google Apps Script to make a Google Form display randomized text from a Google Sheet? 如何从 google-apps-script 中的单页模板生成多页文本文档? - How can I generate a multipage text document from a single page template in google-apps-script? 我如何从谷歌应用程序脚本中解析表值 - How I can parser table values from google apps script 如何从 Google Apps 脚本输出 HTML? - How can I output HTML from a Google Apps Script? 如何停止在已暂停的Google Apps帐户上运行google-apps-script? - How can I stop a google-apps-script from running on a suspended Google Apps account? 如何在 Google Apps 脚本中解决这个问题? - How can I solve this in Google Apps Script? 使用 Google Apps 脚本,如何替换 Google 表格模板中的文本以制作新表格? - Using Google Apps Script, how can I replace text in a Google Sheets template to make a new Sheet? 如何使用 Google Apps 脚本在 Google 表单中添加选项 - How can I add choices in a Google Form with Google Apps Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM