[英]Using google speech API in C# returns 403 error
尝试在C#中使用Google Speech API会返回403。
在Google Cloud Platform中,我生成了一个密钥,但仍然收到403错误。
使用此代码:
class Program
{
static void Main(string[] args)
{
try
{
FileStream fileStream = File.OpenRead("good-morning-google.flac");
MemoryStream memoryStream = new MemoryStream();
memoryStream.SetLength(fileStream.Length);
fileStream.Read(memoryStream.GetBuffer(), 0, (int)fileStream.Length);
byte[] BA_AudioFile = memoryStream.GetBuffer();
HttpWebRequest _HWR_SpeechToText = null;
_HWR_SpeechToText =
(HttpWebRequest)HttpWebRequest.Create(
"https://www.google.com/speech-api/v2/recognize?output=json&lang=en-us&key=YOUR_API_KEY_HERE");
_HWR_SpeechToText.Credentials = CredentialCache.DefaultCredentials;
_HWR_SpeechToText.Method = "POST";
_HWR_SpeechToText.ContentType = "audio/x-flac; rate=44100";
_HWR_SpeechToText.ContentLength = BA_AudioFile.Length;
Stream stream = _HWR_SpeechToText.GetRequestStream();
stream.Write(BA_AudioFile, 0, BA_AudioFile.Length);
stream.Close();
HttpWebResponse HWR_Response = (HttpWebResponse)_HWR_SpeechToText.GetResponse();
if (HWR_Response.StatusCode == HttpStatusCode.OK)
{
StreamReader SR_Response = new StreamReader(HWR_Response.GetResponseStream());
Console.WriteLine(SR_Response.ReadToEnd());
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.ReadLine();
}
}
可能是某些无效的密钥问题,尝试生成服务器密钥和浏览器密钥,结果相同,为403(禁止)
请帮忙。
这似乎不再起作用。 语音API未显示。
我没有尝试使用http API,但这是我使用Google Speech API库(Google.Apis.CloudSpeechAPI.v1beta1)的工作代码:
void Main()
{
//create the service and auth
CloudSpeechAPIService service = new CloudSpeechAPIService(new BaseClientService.Initializer
{
ApplicationName = "Speech API Test",
ApiKey = "your API key..."
});
//configure the audio file properties
RecognitionConfig sConfig = new RecognitionConfig();
sConfig.Encoding = "FLAC";
sConfig.SampleRate = 16000;
sConfig.LanguageCode = "en-AU";
//make the request and output the transcribed text to console
SyncRecognizeResponse response = getResponse(service, sConfig, "audio file.flac");
string resultText = response.Results.ElementAt(0).Alternatives.ElementAt(0).Transcript;
Console.WriteLine(resultText);
}
public SyncRecognizeResponse getResponse(CloudSpeechAPIService sService, RecognitionConfig sConfig, string fileName)
{
//read the audio file into a base 64 string and add to API object
RecognitionAudio sAudio = new RecognitionAudio();
byte[] audioBytes = getAudioStreamBytes(fileName);
sAudio.Content = Convert.ToBase64String(audioBytes);
//create the request with the config and audio files
SyncRecognizeRequest sRequest = new SyncRecognizeRequest();
sRequest.Config = sConfig;
sRequest.Audio = sAudio;
//execute the request
SyncRecognizeResponse response = sService.Speech.Syncrecognize(sRequest).Execute();
return response;
}
public byte[] getAudioStreamBytes(string fileName)
{
FileStream fileStream = File.OpenRead(fileName);
MemoryStream memoryStream = new MemoryStream();
memoryStream.SetLength(fileStream.Length);
fileStream.Read(memoryStream.GetBuffer(), 0, (int)fileStream.Length);
byte[] BA_AudioFile = memoryStream.GetBuffer();
return BA_AudioFile;
}
遇到相同的403错误,以下是帮助我修复的原因
步骤1-使用的快速入门语音API文档-https://cloud.google.com/speech/docs/getting-started ,发现计费已被禁用。 以下是我作为curl输出收到的详细错误消息,即文档中的第3步
{
"error": {
"code": 403,
"message": "Project xxxxx (#xxxxxx) has billing disabled. Please enable it.",
"status": "PERMISSION_DENIED",
"details": [
{
"@type": "type.googleapis.com/google.rpc.Help",
"links": [
{
"description": "Google developer console API key",
"url": "https://console.developers.google.com/project/1026744225026/apiui/credential"
}
]
}
]
}
}
已为项目启用结算。
第2步-确保您是chromium-dev@chromium.org的成员,即按照http://www.chromium.org/developers/how-tos/api-keys的第1步(另请参阅https://github.com。 com / gillesdemey / google-speech-v2 / issues / 8 )
成为会员后,进入您的项目->单击启用API->搜索语音API,它将有2个结果->启用“语音API专用API”(在订阅组之前,您现在只会看到“ Google Cloud Speech API”可以看到“语音API专用API”)
希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.