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,392 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,375 questions
{count} votes

Accepted answer
  1. YutongTie-MSFT 46,566 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-26T23:05:43.92+00:00

    Thank You, I can confirm that it works flawlessly now

    0 comments No comments

  2. Kulveer Singh 1 Reputation point
    2021-09-23T02:12:56.213+00:00

    any idea why we are getting this error ?

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

    0 comments No comments

  3. prateek 0 Reputation points
    2023-06-16T13:16:20.85+00:00

    after replacing region with correct region and your subscrption key with key 1 in curl command, i am getting below error of syntax's. can any one suggest, what i am doing wrong

    Invoke-WebRequest : Cannot bind parameter 'Headers'. Cannot convert the "Ocp-Apim-Subscription-Key: " value of type "System.String" to type
    "System.Collections.IDictionary".
    At line:1 char:86
    + ... oft.com/sts/v1.0/issueToken" -H "Ocp-Apim-Subscription-Key: "4e968 ...
    +                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException
        + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
    
    0 comments No comments

  4. 浩瑋 曾 0 Reputation points
    2024-01-18T11:38:18.3133333+00:00

    same issue, nodejs tutorial not working nodejs v18.16.0 "microsoft-cognitiveservices-speech-sdk": "^1.34.0",

    const fs = require("fs");
    const sdk = require("microsoft-cognitiveservices-speech-sdk");
    
    
    CANCELED: Reason=0
    CANCELED: ErrorCode=4
    CANCELED: ErrorDetails=Unable to contact server. StatusCode: 1006, undefined Reason:  Unexpected server response: 400
    CancellationDetails {
      privReason: 0,
      privErrorDetails: 'Unable to contact server. StatusCode: 1006, undefined Reason:  Unexpected server response: 400',
      privErrorCode: 4
    }
    
    
    0 comments No comments