question

PierreLouisVENTRE-4388 avatar image
2 Votes"
PierreLouisVENTRE-4388 asked KulveerSingh-1083 answered

Speech Cognitive services Authentication error (401) even with correct subscription key

Hi
I meet a problem for building a speech to text with Azure. I followed this tutorial

https://docs.microsoft.com/en-us/azure/cognitive-services/speech-service/get-started-speech-to-text?tabs=script%2Cbrowser%2Cwindowsinstall&pivots=programming-language-python

And nothing is possible with my speech config. I set it as following :

 speech_config = speechsdk.SpeechConfig(subscription="{<!-- -->{MY SUBSCRIPTION KEY}}", region="westus")
 speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config)

I tried to re generate key many times, there is nothing to do I always get the error :

  Speech Recognition canceled: CancellationReason.Error
  Error details: WebSocket upgrade failed: Authentication error (401). Please check subscription information and region name.

I read everywhere that I certainly have a mistake in my key, or that my subscription is over, or I have reached the quota limit. But that's not the problem because I try with this method and get a success :

I followed advices of microsoft doc and tried this :

 curl -v -X POST "https://YOUR_REGION.api.cognitive.microsoft.com/sts/v1.0/issueToken" -H "Ocp-Apim-Subscription-Key: YOUR_SUBSCRIPTION_KEY" -H "Content-type: application/x-www-form-urlencoded" -H "Content-Length: 0"

And it worked, I got a token with success
Then I tried :

 curl -v -X POST "https://YOUR_REGION.stt.speech.microsoft.com/speech/recognition/interactive/cognitiveservices/v1?language=en-US" -H "Authorization: Bearer YOUR_ACCESS_TOKEN" -H "Transfer-Encoding: chunked" -H "Content-type: audio/wav; codec=audio/pcm; samplerate=16000" --data-binary @YOUR_AUDIO_FILE

And I got in text my audio_file
So that's not a problem of subscription, my key is valid.
So if anyone could help me to resolve this "Authentication Error Please check subscription" problem it would be amazing because it's making me mad since 3 days

Thank's for reading



azure-cognitive-servicesazure-speech
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello,

I can reproduce your issue and I have forwarded this issue to product team for investigation. I will let you know once I have any update. Sorry for the experience and thank you for the reaching out.

Regards,
Yutong

3 Votes 3 ·

I'm facing the same issue but with Python.
Curl works.
But Python program fails.
I'm using the API to demonstrate API usage on Monday.

What can I do to demonstrate the API successfully?

0 Votes 0 ·
YutongTie-MSFT avatar image
0 Votes"
YutongTie-MSFT answered PierreLouisVENTRE-4388 commented

Hello everyone,

This works for me now. Please check my code sample and let me know if you still have issue.

 # Copyright (c) Microsoft. All rights reserved.
 # Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
    
 # <code>
 import  azure.cognitiveservices.speech as speechsdk
    
 # Creates an instance of a speech config with specified subscription key and service region.
 # Replace with your own subscription key and service region (e.g., "westus").
 speech_config = speechsdk.SpeechConfig(subscription="1fac40698f86************", region="westus")
    
 # Creates a recognizer with the given settings
 speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config)
 speech_config.set_property(speechsdk.PropertyId.Speech_LogFilename, "LogfilePathAndName")
    
 print("Say something...please")
    
    
 # Starts speech recognition, and returns after a single utterance is recognized. The end of a
 # single utterance is determined by listening for silence at the end or until a maximum of 15
 # seconds of audio is processed.  The task returns the recognition text as result. 
 # Note: Since recognize_once() returns only a single utterance, it is suitable only for single
 # shot recognition like command or query. 
 # For long-running multi-utterance recognition, use start_continuous_recognition() instead.
 result = speech_recognizer.recognize_once()
    
 # Checks result.
 if result.reason == speechsdk.ResultReason.RecognizedSpeech:
     print("Recognized: {}".format(result.text))
 elif result.reason == speechsdk.ResultReason.NoMatch:
     print("No speech could be recognized: {}".format(result.no_match_details))
 elif result.reason == speechsdk.ResultReason.Canceled:
     cancellation_details = result.cancellation_details
     print("Speech Recognition canceled: {}".format(cancellation_details.reason))
     if cancellation_details.reason == speechsdk.CancellationReason.Error:
         print("Error details: {}".format(cancellation_details.error_details))
 # </code>

71721-image.png


Regards,
Yutong



image.png (11.2 KiB)
· 8
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I tried it and I'm getting the same error

Say something...please
Speech Recognition canceled: CancellationReason.Error
Error details: WebSocket upgrade failed: Authentication error (401). Please check subscription information and region name.

0 Votes 0 ·

Could you please share the log? Thanks.

0 Votes 0 ·
bjr5 avatar image bjr5 YutongTie-MSFT ·

Where do I get the logs?
I'm new to Azure

0 Votes 0 ·
Show more comments
bjr5 avatar image bjr5 YutongTie-MSFT ·

The log file

71723-speech-log.txt


0 Votes 0 ·
speech-log.txt (89.3 KiB)

Thank you very much this code worked perfectly for me !

But I notice that my code (which didn't work yesterday) works perfectly too now

0 Votes 0 ·
JarnoEnsioHakulinen-7470 avatar image
0 Votes"
JarnoEnsioHakulinen-7470 answered PierreLouisVENTRE-4388 commented

Hi,

In your example, I see you have used { } around the key, what is the reason for that ?

Please do the call without those.
speech_config = speechsdk.SpeechConfig(subscription="copy_paste_your_key_here", region="westus")

And that should work ok.

Regards,
Jarno

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Yes actually I just wrote { { }} for not sharing my key

0 Votes 0 ·
YutongTie-MSFT avatar image
0 Votes"
YutongTie-MSFT answered

@JarnoEnsioHakulinen-7470 Could you please share your workable sample? I am using below sample but same error happens.

 import azure.cognitiveservices.speech as speechsdk
    
 # Creates an instance of a speech config with specified subscription key and service region.
 # Replace with your own subscription key and service region (e.g., "westus").
 speech_config = speechsdk.SpeechConfig(subscription="a25efc907e11498cb***********", region="westus")
    
 # Creates a recognizer with the given settings
 speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config)
    
 print("Say something...")
 result = speech_recognizer.recognize_once()
    
 # Checks result.
 if result.reason == speechsdk.ResultReason.RecognizedSpeech:
     print("Recognized: {}".format(result.text))
 elif result.reason == speechsdk.ResultReason.NoMatch:
     print("No speech could be recognized: {}".format(result.no_match_details))
 elif result.reason == speechsdk.ResultReason.Canceled:
     cancellation_details = result.cancellation_details
     print("Speech Recognition canceled: {}".format(cancellation_details.reason))
     if cancellation_details.reason == speechsdk.CancellationReason.Error:
         print("Error details: {}".format(cancellation_details.error_details))
 # </code>

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JarnoEnsioHakulinen-7470 avatar image
0 Votes"
JarnoEnsioHakulinen-7470 answered YutongTie-MSFT commented

@YutongTie-5848 I just tried your sample and that worked ok with 1.15.0 speech sdk version.

Could you tell me what is your Python version, python --version?

Regards,
Jarno

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I am on Python 3.8.7

0 Votes 0 ·
JarnoEnsioHakulinen-7470 avatar image
0 Votes"
JarnoEnsioHakulinen-7470 answered

What is the operating system ? Could you provide logs as described here:
https://docs.microsoft.com/en-us/azure/cognitive-services/speech-service/how-to-use-logging

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MateuszTondos-2161 avatar image
0 Votes"
MateuszTondos-2161 answered MateuszTondos-2161 edited

Same happening to me for cognitive-services-speech-sdk Node.js quickstart tutorial -
https://github.com/Azure-Samples/cognitive-services-speech-sdk/tree/master/quickstart/javascript/node/from-file
I also checked token by:

 curl -v -X POST "https://westeurope.api.cognitive.microsoft.com/sts/v1.0/issueToken" -H "Ocp-Apim-Subscription-Key: <my key>" -H "Content-type: application/x-www-form-urlencoded" -H "Content-Length: 0"


which properly returned 200 with new token, however the recognizeOnceAsync returns Unauthenticated 401 code:

 SpeechRecognitionResult {
   privResultId: '193B48A21A994C568302EAEB95C674EE',
   privReason: 1,
   privText: undefined,
   privDuration: undefined,
   privOffset: undefined,
   privLanguage: undefined,
   privLanguageDetectionConfidence: undefined,
   privErrorDetails: 'Unable to contact server. StatusCode: 1006, undefined Reason:  Unexpected server response: 401',
   privJson: undefined,
   privProperties: PropertyCollection {
     privKeys: [ 'CancellationErrorCode' ],
     privValues: [ 'ConnectionFailure' ]
   },
   privSpeakerId: undefined
 }

I am using westeurope region & key 2 listed in Azure RESOURCE MANAGEMENT > Keys and Endpoint, on MacOS Bug Sur
I checked audio file properties: 16000 bitrate & mono

@edit - using sdk.SpeechConfig.fromAuthorizationToken() instead of fromSubscription() with hard-coded bearer token retrieved from above curl works

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

jacqueskang avatar image
0 Votes"
jacqueskang answered jacqueskang commented

I have same issue in westeurope region.
I'm sure my subscription key and region are correct but still getting 401 error.

My codes C# below:

         public async Task<string> TranslationWithAudioStreamAsync(Stream audioStream, string fromLanguage = "zh-CN", string targetLanguage = "en-US")
         {
             var config = SpeechTranslationConfig.FromSubscription(_subscriptionKey, _region);
             config.SpeechRecognitionLanguage = fromLanguage;
             config.AddTargetLanguage(targetLanguage);
    
             _log.LogInformation($"SubscriptionKey: '{config.SubscriptionKey}'");
             _log.LogInformation($"Region: '{config.Region}'");
             _log.LogInformation($"AuthorizationToken: '{config.AuthorizationToken}'");
             _log.LogInformation($"SpeechRecognitionLanguage: '{config.SpeechRecognitionLanguage}'");
             _log.LogInformation($"TargetLanguages[0]: '{config.TargetLanguages[0]}'");
    
             var stopTranslation = new TaskCompletionSource<int>();
    
             string translateResult = null;
    
             // Create an audio stream from a wav file.
             // Replace with your own audio file name.
             using (var audioInput = OpenWavFile(audioStream))
             using (var recognizer = new TranslationRecognizer(config, audioInput))
             {
                 // Subscribes to events.
                 recognizer.Recognizing += (s, e) =>
                 {
                     _log.LogInformation($"RECOGNIZING in '{fromLanguage}': Text = {e.Result.Text}");
                     foreach (var element in e.Result.Translations)
                     {
                         _log.LogInformation($"    TRANSLATING into '{element.Key}': {element.Value}");
                     }
                 };
    
                 recognizer.Recognized += (s, e) =>
                 {
                     if (e.Result.Reason == ResultReason.TranslatedSpeech)
                     {
                         _log.LogInformation($"RECOGNIZED in '{fromLanguage}': Text={e.Result.Text}");
                         foreach (var element in e.Result.Translations)
                         {
                             _log.LogInformation($"    TRANSLATED into '{element.Key}': {element.Value}");
                             translateResult = element.Value;
                         }
                     }
                     else if (e.Result.Reason == ResultReason.RecognizedSpeech)
                     {
                         _log.LogInformation($"RECOGNIZED: Text={e.Result.Text}");
                         _log.LogInformation($"    Speech not translated.");
                     }
                     else if (e.Result.Reason == ResultReason.NoMatch)
                     {
                         _log.LogInformation($"NOMATCH: Speech could not be recognized.");
                     }
                 };
    
                 recognizer.Canceled += (s, e) =>
                 {
                     _log.LogInformation($"CANCELED: Reason={e.Reason}");
    
                     if (e.Reason == CancellationReason.Error)
                     {
                         _log.LogInformation($"CANCELED: ErrorCode={e.ErrorCode}");
                         _log.LogInformation($"CANCELED: ErrorDetails={e.ErrorDetails}");
                         _log.LogInformation($"CANCELED: Did you update the subscription info?");
                     }
    
                     stopTranslation.TrySetResult(0);
                 };
    
                 recognizer.SpeechStartDetected += (s, e) =>
                 {
                     _log.LogInformation("\nSpeech start detected event.");
                 };
    
                 recognizer.SpeechEndDetected += (s, e) =>
                 {
                     _log.LogInformation("\nSpeech end detected event.");
                 };
    
                 recognizer.SessionStarted += (s, e) =>
                 {
                     _log.LogInformation("\nSession started event.");
                 };
    
                 recognizer.SessionStopped += (s, e) =>
                 {
                     _log.LogInformation($"\nSession stopped event.");
                     _log.LogInformation($"\nStop translation.");
                     stopTranslation.TrySetResult(0);
                 };
    
                 // Starts continuous recognition. Uses StopContinuousRecognitionAsync() to stop recognition.
                 _log.LogInformation("Start translation...");
                 await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false);
    
                 // Waits for completion.
                 // Use Task.WaitAny to keep the task rooted.
                 Task.WaitAny(new[] { stopTranslation.Task });
    
                 // Stops translation.
                 await recognizer.StopContinuousRecognitionAsync().ConfigureAwait(false);
    
    
                 if (translateResult == null)
                 {
                     throw new Exception("Translation failed.");
                 }
    
                 return translateResult;
             }
         }


Results:

 2021-02-25T08:05:16.425 [Information] Processing voice...
 2021-02-25T08:05:16.426 [Information] Target: 'Jacques', size: 16044 bytes. Translating...
 2021-02-25T08:05:18.674 [Information] SubscriptionKey: '18d9dd783****************'
 2021-02-25T08:05:18.675 [Information] Region: 'westeurope'
 2021-02-25T08:05:18.675 [Information] AuthorizationToken: ''
 2021-02-25T08:05:18.675 [Information] SpeechRecognitionLanguage: 'zh-CN'
 2021-02-25T08:05:18.676 [Information] TargetLanguages[0]: 'en-US'
 2021-02-25T08:05:18.880 [Information] Start translation...
 2021-02-25T08:05:19.220 [Information] Session started event.
 2021-02-25T08:05:19.236 [Information] CANCELED: Reason=Error
 2021-02-25T08:05:19.236 [Information] CANCELED: ErrorCode=AuthenticationFailure
 2021-02-25T08:05:19.236 [Information] CANCELED: ErrorDetails=WebSocket upgrade failed: Authentication error (401). Please check subscription information and region name. SessionId: cbfdf9f65a08465d9dc56df5730615b9
 2021-02-25T08:05:19.236 [Information] CANCELED: Did you update the subscription info?
 2021-02-25T08:05:19.239 [Information] Session stopped event.
 2021-02-25T08:05:19.239 [Information] Stop translation.


· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@jacqueskang Do you still see the issue?

0 Votes 0 ·
jacqueskang avatar image jacqueskang JarnoEnsioHakulinen-7470 ·

Yes I just tested. Same error

0 Votes 0 ·
jacqueskang avatar image jacqueskang JarnoEnsioHakulinen-7470 ·

And now it's fixed!

0 Votes 0 ·
JarnoEnsioHakulinen-7470 avatar image
0 Votes"
JarnoEnsioHakulinen-7470 answered KulveerSingh-1083 commented

Thanks for feedback! We are aware of problem in subscription activation delay and we are deploying fix at the moment.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I see same error . How much time subscription activation takes ?

0 Votes 0 ·
MateuszTondos-2161 avatar image
0 Votes"
MateuszTondos-2161 answered

Thank You, I can confirm that it works flawlessly now

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

KulveerSingh-1083 avatar image
0 Votes"
KulveerSingh-1083 answered

any idea why we are getting this error ?

Speech Cognitive services Authentication error (401) even with correct subscription key

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.