嘗試使用谷歌語音API在C#返回403使用谷歌語音API返回403錯誤
在谷歌雲平臺我生成密鑰,並仍然得到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(禁止)
請幫助。
您是否嘗試過使用客戶端庫,而不是直接使用'HttpWebRequest'進行調用? (試圖找到正確的版本 - https://www.nuget.org/packages/Google.Apis.CloudSpeechAPI.v1beta1/適用於v1beta1 ...這可能是我們正在尋找不同的API。) –
謝謝@JonSkeet,我會試試看。 –
@JonSkeet,你有一個使用Google.Apis.CloudSpeechAPI.v1beta1的工作示例嗎?或甚至使用這個庫的一些例子?我想使用一個密鑰而不是oauth2 –