我正在尝试使用以下 url 访问语音 api,并且总是收到 403(禁止)错误,无效密钥。
https://www.google.com/speech-api/v2/recognize?output=json&lang=en-us&key= {MyKey}
我已尝试使用服务器密钥和浏览器密钥,如此密钥图像所示。
我正在使用 .NET HTTP 客户端发送 http 请求;代码片段如下:
Stream stream = null;
StreamReader sr = null;
WebResponse response = null;
JSon.RecognizedItem result;
try
{
WebRequest request = WebRequest.Create(Constants.GoogleRequestString);
request.Method = "POST";
request.ContentType = "audio/x-flac; rate=" + sampleRate;
request.ContentLength = bytes.Length;
stream = request.GetRequestStream();
stream.Write(bytes, 0, bytes.Length);
stream.Close();
response = request.GetResponse();
stream = response.GetResponseStream();
if (stream == null)
{
throw new Exception("Can't get a response from server. Response stream is null.");
}
sr = new StreamReader(stream);
//Get response in JSON format
string respFromServer = sr.ReadToEnd();
var parsedResult = JSon.Parse(respFromServer);
result =
parsedResult.hypotheses.Where(d => d.confidence == parsedResult.hypotheses.Max(p => p.confidence)).FirstOrDefault();
}
finally
{
if (stream != null)
stream.Close();
if (sr != null)
sr.Close();
if (response != null)
response.Close();
}
return result == null ? "" : result.utterance;
任何人都可以帮我找出这里的问题。
谢谢,阿布。