-
Notifications
You must be signed in to change notification settings - Fork 26
403 Error code for cs sample #9
Description
Hello I am trying to implement the REST STT in asp.net core app and I get a 403 forbidden code:
Here is my relevant code:
`STTModel sttModel = new STTModel();
string host = @"westus.api.cognitive.microsoft.com";
string contentType = @"audio/wav; codec=""audio/pcm""; samplerate=16000";
string requestUri = "https://speech.platform.bing.com/speech/recognition/conversation/cognitiveservices/v1?language=en-US&format=detailed";// args[0];
// string requestUri = "https://westus.stt.speech.microsoft.com/speech/recognition/conversation/cognitiveservices/v1?language=en-US&format=detailed";
///*
// * Input your own audio file or use read from a microphone stream directly.
// */
string audioFile = destFile; //args[1];
string responseString;
FileStream fs = null;
try
{
var token = sttModel.GetAccessToken();
Console.WriteLine("Token: {0}\n", token);
Console.WriteLine("Request Uri: " + requestUri + Environment.NewLine);
var request = (HttpWebRequest)WebRequest.Create(requestUri);
request.SendChunked = true;
request.Accept = @"application/json;text/xml";
request.Method = "POST";
request.ProtocolVersion = HttpVersion.Version11;
request.Host = host;
request.ContentType = contentType;
request.Headers["Authorization"] = "Bearer " + token;
//request.Expect = "100-continue";
//request.Headers["Ocp-Apim-Subscription-Key"] = "09ae100b29fe4defb6fe7f0519040889";
using (fs = new FileStream(audioFile, FileMode.Open, FileAccess.Read))
{
/*
* Open a request stream and write 1024 byte chunks in the stream one at a time.
*/
byte[] buffer = null;
int bytesRead = 0;
using (Stream requestStream = request.GetRequestStream())
{
/*
* Read 1024 raw bytes from the input audio file.
*/
buffer = new Byte[checked((uint)Math.Min(1024, (int)fs.Length))];
while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) != 0)
{
requestStream.Write(buffer, 0, bytesRead);
}
// Flush
requestStream.Flush();
}
/*
* Get the response from the service.
*/
ServicePointManager
.ServerCertificateValidationCallback +=
(sender, cert, chain, sslPolicyErrors) => true;
Console.WriteLine("Response:");
using (WebResponse response = request.GetResponse())
{
Console.WriteLine("Responnse:");
Console.WriteLine(((HttpWebResponse)response).StatusCode);
Console.WriteLine("Responnnnse:");
using (StreamReader sr = new StreamReader(response.GetResponseStream() ?? throw new InvalidOperationException()))
{
responseString = sr.ReadToEnd();
}
Console.WriteLine(responseString);
Console.ReadLine();
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.WriteLine(ex.Message);
Console.ReadLine();
}
`