首页 新闻 搜索 专区 学院

腾讯云 c#图片识别时的鉴权签名问题

0
[已关闭问题] 关闭于 2018-12-04 16:55

APPID、Secret ID 和 Secret Key 使用的就是在API密钥管理获取到的,数据万象也注册了

在提交时一直提示返回

{
"code": 9,
"message": "sign expired"
}

不知道是哪里的问题

实例代码:

/// <summary>
/// request提交
/// </summary>
/// <param name="body"></param>
/// <returns></returns>
protected async Task<string> ImageRequestAsync(string host,string[] imgurl_list)
{
if (imgurl_list != null && imgurl_list.Length > 0)
{
var appid = "appid";
var secretid = "secretid";
var secretkey = "secretKey";

      var url = new Uri(host);
      var body = new
      {
          appid,
          url_list = imgurl_list
      };
      var cookieContainer = new CookieContainer();
      // 将提交的字符串数据转换成字节数组  
      byte[] postData = System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(body));
      // 设置提交的相关参数  
      var myEncoding = System.Text.Encoding.GetEncoding("utf-8");
      var request = WebRequest.Create(url) as HttpWebRequest;
      request.Method = "POST";
      request.KeepAlive = false;
      request.AllowAutoRedirect = true;
      request.Host = "recognition.image.myqcloud.com";
      request.ContentType = "application/json";
      request.Headers.Add("Authorization", AppSign(appid, secretid, secretkey));
      request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
      request.CookieContainer = cookieContainer;
      request.ContentLength = postData.Length;

      // 提交请求数据  
      var outputStream = request.GetRequestStream();
      outputStream.Write(postData, 0, postData.Length);
      outputStream.Close();

      var response = request.GetResponse() as HttpWebResponse;
      var responseStream = response.GetResponseStream();

      var reader = new StreamReader(responseStream, System.Text.Encoding.UTF8);
      var responseData = reader.ReadToEnd();
      reader.Close();
      return responseData;// 解码
 }
 return "";

}
private static string AppSign(string appId, string secretId, string secretKey)
{
var ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
var currentTime = Convert.ToInt64(ts.TotalSeconds);//当前时间戳,是一个符合 UNIX Epoch 时间戳规范的数值,单位为秒,多次签名时,e 应大于 t
var expiredTime = Convert.ToInt64((DateTime.UtcNow.AddMinutes(180) - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds);//签名的有效期,是一个符合 UNIX Epoch 时间戳规范的数值,单位为秒;单次签名时,e 必须设置为 0
string plainText = $"a={appId}&b=&k={secretId}&e={currentTime}&t={expiredTime}&r={GetRandom()}&f=";
return Hmacsha1Encrypt(plainText, secretKey);
}
/// <summary>
/// HMACSHA1算法加密
/// </summary>
private static string Hmacsha1Encrypt(string encryptText, string encryptKey)
{
using (HMACSHA1 mac = new HMACSHA1(Encoding.UTF8.GetBytes(encryptKey)))
{
var hash = mac.ComputeHash(Encoding.UTF8.GetBytes(encryptText));
var pText = Encoding.UTF8.GetBytes(encryptText);
var all = new byte[hash.Length + pText.Length];
Array.Copy(hash, 0, all, 0, hash.Length);
Array.Copy(pText, 0, all, hash.Length, pText.Length);
return Convert.ToBase64String(all);
}
}
/// <summary>
/// 获取随机数
/// </summary>
private static int GetRandom()
{
var random = new Random();
var rand = random.Next(10000, 999999999);
return rand;
}

wpzblog的主页 wpzblog | 初学一级 | 园豆:30
提问于:2018-12-04 15:25
< >
分享
所有回答(1)
0

大兄弟感谢你提供的参考,问题解决了吧

ice.ko | 园豆:164 (初学一级) | 2019-01-06 23:16

解决了,是
string plainText = $"a={appId}&b=&k={secretId}&e={currentTime}&t={expiredTime}&r={GetRandom()}&f=";
这一段,appid和secretid写反了,你使用的时候核对以下对应关系

支持(0) 反对(0) wpzblog | 园豆:30 (初学一级) | 2019-01-07 13:21
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册