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

Pierre-Louis VENTRE 46 Reputation points
2021-02-23T18:14:25.14+00:00

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

https://learn.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 AI Speech
Azure AI Speech
An Azure service that integrates speech processing into apps and services.
1,391 questions
Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
2,362 questions
{count} votes

Accepted answer
  1. YutongTie-MSFT 46,326 Reputation points
    2021-02-24T17:14:43.507+00:00

    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

    1 person found this answer helpful.

13 additional answers

Sort by: Most helpful
  1. Jarno Ensio Hakulinen 1 Reputation point Microsoft Employee
    2021-02-24T01:49:35.033+00:00

    @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


  2. Jarno Ensio Hakulinen 1 Reputation point Microsoft Employee
    2021-02-24T02:10:12.703+00:00

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

    0 comments No comments

  3. Jacques Kang 6 Reputation points
    2021-02-25T08:13:22.52+00:00

    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.
    

  4. Jarno Ensio Hakulinen 1 Reputation point Microsoft Employee
    2021-02-25T16:51:08.293+00:00

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