最近有个需求需要调用腾讯语音合成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";