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,378 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,344 questions
{count} votes

Accepted answer
  1. YutongTie-MSFT 45,911 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. Mateusz Tondos 6 Reputation points
    2021-02-25T00:55:36.703+00:00

    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

    1 person found this answer helpful.
    0 comments No comments

  2. Brian Chen 5 Reputation points
    2024-01-04T19:44:00.5233333+00:00

    By doing trial and error, I found that I got the error when I use the Free Tier (F0), but as soon as I switch to a Paid Tier (S0), then it works. It has noting to do with subscription key nor region setting.

    1 person found this answer helpful.
    0 comments No comments

  3. Jarno Ensio Hakulinen 1 Reputation point Microsoft Employee
    2021-02-24T01:32:53.57+00:00

    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


  4. YutongTie-MSFT 45,911 Reputation points
    2021-02-24T01:44:11.09+00:00

    @Jarno Ensio Hakulinen 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>  
    
    0 comments No comments