尝试在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(禁止)
请帮忙。
答案 0 :(得分:1)
这似乎不再适用了。语音API无法显示。
答案 1 :(得分:0)
我还没有尝试过使用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;
}
答案 2 :(得分:0)
获得相同的403错误,以下是帮助我解决的问题
第1步 - 使用快速入门语音API文档 - https://cloud.google.com/speech/docs/getting-started,发现结算已被停用。下面是我收到的作为卷曲输出的详细错误消息,即文档中的第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/gillesdemey/google-speech-v2/issues/8)
成为会员后转到您的项目 - >点击启用API - >搜索语音API,它将有2个结果 - >启用“Speech API私有API”(在订阅群组之前,您只能看到'Google Cloud Speech API',现在您可以看到'Speech API私有API')
希望这有帮助