查看文档,它提供了一个使用 curl 的示例:
https://cloud.google.com/speech-to-text/docs/quickstart-protocol
您需要创建一个项目和一个属于该项目的服务帐户,以便将您的请求绑定到可能计费的资源。
然后,您可以使用服务帐户的应用程序默认凭据,使用 curl,您需要gcloud auth application-default login
(使用服务帐户)然后gcloud auth application-default print-access-token
获取 curl 的不记名令牌。
2021-01-11 更新
以下是完整的步骤:
PROJECT=[[YOUR-PROJECT]]
BILLING=[[YOUR-BILLING]]
ACCOUNT=[[YOUR-ACCOUNT]] # e.g. roboto
ADDRESS=${ROBOT}@${PROJECT}.iam.gserviceaccount.com
gcloud projects create ${PROJECT}
gcloud beta billing projects link ${PROJECT} \
--billing-account=${BILLING}
# Enable speech-to-text service
gcloud services enable speech.googleapis.com \
--project=${PROJECT}
# Create Service Account
gcloud iam service-accounts create ${ACCOUNT} \
--project=${PROJECT}
# NB The Account needs no roles|permissions
# Create Service Account Key
gcloud iam service-accounts keys create ./${ROBOT}.json \
--iam-account=${EMAIL} \
--project=${PROJECT}
# Export for Application Default Creds
export GOOGLE_APPLICATION_CREDENTIALS="${PWD}/${ROBOT}.json"
# Call it
TOKEN=$(gcloud auth application-default print-access-token)
curl \
--silent \
--header "Content-Type: application/json" \
--header "Authorization: Bearer ${TOKEN}" \
https://speech.googleapis.com/v1/speech:recognize \
--data '{ "config": { "encoding":"FLAC", "sampleRateHertz": 16000, "languageCode": "en-US", "enableWordTimeOffsets": false }, "audio": { "uri":"gs://cloud-samples-tests/speech/brooklyn.flac" } }'
产量:
{
"results": [
{
"alternatives": [
{
"transcript": "how old is the Brooklyn Bridge",
"confidence": 0.9823954
}
]
}
]
}