代码 403:“请求缺少有效的 API 键。” PERMISSION_DENIED 服务帐户密钥 Google python 中的云语音转文本 api”

Code 403 : “The request is missing a valid API key.” PERMISSION_DENIED Service Account Key Google Cloud speech to text api in python"

我想使用服务帐户实现 Google 云语音到文本。我尝试的是将环境变量设置为 json 并将 post 请求发送到此 url 'https://speech.googleapis.com/v1/speech:longrunningrecognize'.

代码: req = requests.post(url, 数据={ "audio":{ "content":enc }, "config":{ "audioChannelCount":2, "enableSeparateRecognitionPerChannel":是的, "enableWordTimeOffsets":是的, "diarizationConfig":{ "enableSpeakerDiarization": 是的, "minSpeakerCount": 1, "maxSpeakerCount": 2 },

}}) 错误: 403 { "error":{ "code": 403, "message": "The request is missing a valid API key.", "status": "PERMISSION_DENIED" } }

错误消息表明您没有正确验证。这样做的方法是在您的请求中将身份验证令牌作为 Bearer Token header 传递。

以下文档解释了如何生成所需的凭据并将其与请求一起传递,这提供了服务帐户的概述Service accounts overview

创建服务帐户说明Creating service accounts

创建服务帐户后,您将生成以 json 格式存储的凭据,然后将这些凭据作为 Bearer Token

传递