You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure语音转文本401认证错误求助:多语言调用均失败

Azure语音转文本401认证错误排查方案

核心排查步骤

  • 验证密钥与区域匹配

    • 确认SPEECH_KEY是Azure语音专属资源的密钥(不是通用认知服务密钥,多服务密钥可能存在权限限制),从Azure门户「密钥和终结点」页面复制完整密钥,避免多余空格或换行。
    • SPEECH_REGION必须用Azure标准区域标识符(如eastus、southeastasia),不能用中文名称,且与创建语音资源时选择的区域完全一致。
  • 检查资源网络与状态

    • 登录Azure门户查看语音资源状态,确保无异常停用提示。免费版配额不足会返回429,而非401,可排除配额问题。
    • 进入资源「网络」设置,确认是否允许“所有网络”访问。若配置了虚拟网络限制,需将测试IP加入允许列表(私有网络测试时),免费版也支持网络访问控制配置。
  • 排除代码与环境变量问题

    • 代码中打印了COGNITIVE_SERVICE_KEY,但实际使用SPEECH_KEY,需确认SPEECH_KEY环境变量已正确加载——可临时硬编码密钥和区域测试,排除环境变量读取失败的情况:
      speech_config = speechsdk.SpeechConfig(subscription="你的语音资源密钥", region="你的资源区域")
      
    • 确保未混用不同资源的密钥,语音转文本必须使用语音专属资源的密钥。
  • 网络连通性验证

    • 公网环境下,用curl测试REST接口是否能正常访问(替换密钥和区域):
      curl -v -X POST "https://<区域>.stt.speech.microsoft.com/speech/recognition/conversation/cognitiveservices/v1?language=en-US" -H "Ocp-Apim-Subscription-Key: <你的密钥>" -H "Content-Type: audio/wav; codecs=audio/pcm; samplerate=16000" --data-binary "@test.wav"
      
    • 私有网络环境下,检查防火墙或代理是否拦截了WebSocket请求(语音SDK默认使用WebSocket协议),需允许访问*.speech.microsoft.com域名。

用户问题详情

已查阅Stack Overflow相关问答但未找到有效解决方案。运行语音转文本示例代码时,REST、Python、JavaScript三种方式均返回401认证错误,查看日志也未获得有用信息。使用的是免费版服务,在私有网络和公网环境测试均出现该错误,且未找到需配置入站调用的相关文档。

运行的Python代码

import sys
sys.path
import azure.cognitiveservices.speech as speechsdk
print("hello from Azure")
import os
from azure.cognitiveservices.speech.audio import AudioOutputConfig


def recognize_from_microphone():
    print(os.environ.get('SPEECH_KEY'))
    print(os.environ.get('SPEECH_REGION'))
    print(os.environ.get('COGNITIVE_SERVICE_KEY'))
    speech_config = speechsdk.SpeechConfig(subscription=os.environ.get('SPEECH_KEY'), region=os.environ.get('SPEECH_REGION'))
    speech_config.speech_recognition_language="en-US"
    speech_config.set_property(speechsdk.PropertyId.Speech_LogFilename, "LOGS.txt")

    audio_config = speechsdk.audio.AudioConfig(use_default_microphone=True)
    speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)

    print("Speak into your microphone.")
    speech_recognition_result = speech_recognizer.recognize_once()

    # speech_recognition_result = speech_recognizer.recognize_once_async().get()

    if speech_recognition_result.reason == speechsdk.ResultReason.RecognizedSpeech:
        print("Recognized: {}".format(speech_recognition_result.text))
    elif speech_recognition_result.reason == speechsdk.ResultReason.NoMatch:
        print("No speech could be recognized: {}".format(speech_recognition_result.no_match_details))
    elif speech_recognition_result.reason == speechsdk.ResultReason.Canceled:
        cancellation_details = speech_recognition_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))
            print("Did you set the speech resource key and region values?")

recognize_from_microphone()

错误信息

Error details: WebSocket upgrade failed: Authentication error (401). Please check subscription information and region name. SessionId:


内容的提问来源于stack exchange,提问作者that_random_guy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 13:52:13