[英]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 令牌的创建。
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.