Azure Cognitive Service-TTS 401认证错误排查求助
Azure TTS调用401认证错误排查方案
刚注册了免费Azure账户,已反复确认订阅ID正确,但调用Azure TTS的代码执行失败,报错信息:
WebSocket upgrade failed: Authentication error (401). Please check subscription information and region name. USP state: Sending. Received audio size: 0 bytes.
调用代码如下:
public static async Task Start() { var subscriptionKey = "my subscription ID is here"; var region = "westus2"; // I live in San Diego ,CA var config = SpeechConfig.FromSubscription(subscriptionKey, region); config.SpeechSynthesisVoiceName = "en-US-AriaNeural"; using (var audioConfig = AudioConfig.FromWavFileOutput("output.wav")) { using (var synthesizer = new SpeechSynthesizer(config, audioConfig)) { string inputText = "Hello, this is a sample text to be converted to speech. It is not long, but hopefully it is of a great quality" + "since it is generating using Azure SDK. Have a nice day!"; try { using (var result = await synthesizer.SpeakTextAsync(inputText)) { if (result.Reason == ResultReason.SynthesizingAudioCompleted) { Console.WriteLine($"The text has been converted to speech and saved as output.wav"); } else if (result.Reason == ResultReason.Canceled) { var cancellation = SpeechSynthesisCancellationDetails.FromResult(result); Console.WriteLine($"CANCELED: Reason={cancellation.Reason}"); if (cancellation.Reason == CancellationReason.Error) { Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}"); Console.WriteLine($"CANCELED: ErrorDetails=[{cancellation.ErrorDetails}]"); Console.WriteLine($"CANCELED: Did you update the subscription info?"); } } } } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); } } } }
排查方向
- 混淆了订阅ID与语音服务密钥:代码中
subscriptionKey参数需要填写语音服务资源的密钥,而非Azure全局订阅ID。登录Azure门户,找到创建的语音服务资源,进入「密钥和终结点」页面获取Key1或Key2填入即可。 - 资源区域不匹配:确认你创建的语音服务资源所在区域是否为
westus2,代码中的region必须与资源实际区域完全一致,否则会触发认证失败。 - 免费账户资源状态异常:检查语音服务资源是否正常激活,免费账户的语音服务有额度限制,确认配额是否未耗尽、资源未被禁用。
- 网络拦截问题:本地防火墙或代理可能拦截了WebSocket请求,导致认证流程失败,尝试关闭代理或将Azure语音服务相关域名加入白名单。
- SDK版本过旧:使用的Azure Speech SDK版本如果过旧,可能存在认证兼容问题,建议更新至最新稳定版。
内容的提问来源于stack exchange,提问作者Leon
相关产品推荐
相关产品推荐

