WebSocket upgrade failed: Authentication error (401). Please check subscription information and region name
Sanjay Santra
0
Reputation points
I am continuously getting the following error when trying to access Azure speech services from a C# windows forms app:
WebSocket upgrade failed: Authentication error (401). Please check subscription information and region name
- The speech resource is S0 tier
- Registered an Entra ID app and gave permission to Microsoft Cognitive Services
- Under role assignment of speech resource, added the Entra ID app (service principal) with Cognitive Services Speech User and Cognitive Services Speech Contributor permissions
- Created a custom domain name and private endpoint for the speech resource What I am missing out: Here's the code: TokenRequestContext context = new Azure.Core.TokenRequestContext(new string[] { "https://cognitiveservices.azure.com/.default" }); //InteractiveBrowserCredential browserCredential = new InteractiveBrowserCredential(); var credential = new InteractiveBrowserCredential(new InteractiveBrowserCredentialOptions {
}); var browserToken = credential.GetToken(context); string aadToken = browserToken.Token; // Define the custom domain endpoint for your Speech resource. var endpoint = "wss://customdomain.cognitiveservices.azure.com/stt/speech/universal/v2"; string resourceId = "/subscriptions/...."; string region = "eastus"; // You need to include the "aad#" prefix and the "#" (hash) separator between resource ID and Microsoft Entra access token. var authorizationToken = $"aad#{resourceId}#{aadToken}"; SpeechTranslationConfig translationConfig = SpeechTranslationConfig.FromAuthorizationToken(authorizationToken, region); translationConfig.SpeechRecognitionLanguage = "en-US"; translationConfig.AddTargetLanguage("fr"); // English to French // Enable Cognitive Services Logging string logFilePath = "speech-sdk-log.txt"; translationConfig.SetProperty(PropertyId.Speech_LogFilename, logFilePath); Console.WriteLine($"π Logging enabled. Logs will be saved to: {logFilePath}"); using var recognizer = new TranslationRecognizer(translationConfig); Console.WriteLine("Speak something..."); var result = await recognizer.RecognizeOnceAsync(); if (result.Reason == ResultReason.TranslatedSpeech) {TenantId = "", ClientId = ""
} else if (result.Reason == ResultReason.Canceled) {Console.WriteLine($"Original: {result.Text}"); foreach (var (lang, translation) in result.Translations) { Console.WriteLine($"Translated [{lang}]: {translation}"); }
} else {var cancellation = CancellationDetails.FromResult(result); Console.WriteLine($"Speech translation canceled: {cancellation.Reason}"); if (cancellation.Reason == CancellationReason.Error) { Console.WriteLine($"Error Code: {cancellation.ErrorCode}"); Console.WriteLine($"Error Details: {cancellation.ErrorDetails}"); }
}Console.WriteLine($"Speech Recognition Failed: {result.Reason}");
Sign in to answer