腾讯语音合成V3鉴权失败

本文介绍了在使用腾讯语音合成API V3版本时遇到的鉴权失败问题。通过对比ApiExplorer中的签名生成过程,发现本地代码在处理请求头时存在不一致。修复了请求头中的ContentType设置后,成功解决了鉴权失败的错误,实现了接口的正常调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近有个需求需要调用腾讯语音合成Api。使用的V3鉴权,一直提示如下错误

{"Response":{"Error":{"Code":"AuthFailure.SignatureFailure","Message":"The provided credentials could not be validated. Please check your signature is correct."},"RequestId":"60650a2f-e2a3-498b-8af2-fb334a27610a"}}

和官网的ApiExplorer中的生成签名一步步核对,发现和接口文档中的demo的最大区别是接口demo 中编码方式,ApiExplorer中没有,看了一些帖子说是这里上下要保持一致。因为要排查报错原因,这里改成和ApiExplorer中一样。具体代码如下:

private static Dictionary<String, String> BuildHeaders(string secretid,
        string secretkey, string service, string endpoint, string region,
        string action, string version, DateTime date, string requestPayload)
        {
            string datestr = date.ToString("yyyy-MM-dd");
            DateTime startTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
            long requestTimestamp = (long)Math.Round((date - startTime).TotalMilliseconds, MidpointRounding.AwayFromZero) / 1000;//请求时间戳
            // ************* 步骤 1:拼接规范请求串 *************
            string algorithm = "TC3-HMAC-SHA256";//签名算法
            string httpRequestMethod = "POST";
            string canonicalUri = "/";//Api3.0固定为正斜杠(/)
            string canonicalQueryString = "";//对于post请求,固定为字符串""
            string contentType = "application/json";
            //string canonicalHeaders = "content-type:" + contentType + "; charset=utf-8\n" + "host:" + endpoint + "\n";//参与签名的头部信息,至少包含host和content-type两个头部,头部key和value统一转为小写。按照Key:vlaue\n格式拼接;多个头部,按照头部key(小写)的ASCII升序进行拼接
            string canonicalHeaders = "content-type:" + contentType +

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值