该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
自定义客户端
如果要创建自己的客户端,则必须正确创建请求。远程API使用HMAC(HMACSHA1)来计算来自特定数据的散列。HMAC正在使用私钥。不要与任何人分享此密钥。
你必须从字符串(行)计算哈希:
URL - 包括查询参数
Nonce - 随机字符串,只能使用一次,可以是数字
日期 - UtcNow,格式r,RFC1123模式,必须添加到标题
这转换为来自UTF8字符串的字节缓冲区,然后用于计算哈希值。散列转换为Base64字符串,并以“nonce:hash”格式添加到“授权”字段的标题中。
C#中的代码示例(使用RestSharp库):
private readonly string m_remoteUrl =“/ vrageremote / {0}”;
public RestRequest CreateRequest(string resourceLink,Method method,
params Tuple [] queryParams)
{
string methodUrl = string.Format(m_remoteUrl,resourceLink);
RestRequest request = new RestRequest(methodUrl,method);
string date = DateTime.UtcNow.ToString(“r”,CultureInfo.InvariantCulture);
request.AddHeader(“日期”,日期);
m_nonce = random.Next(0,int.MaxValue);
string nonce = m_nonce.ToString();
StringBuilder message = new StringBuilder();
message.Append(methodUrl);
if(queryParams.Length> 0)
{
message.Append( “?”);
}
对(INT I = 0; I
{
VAR PARAM = queryParams [I];
request.AddQueryParameter(param.Item1,param.Item2);
message.AppendFormat(“{0} = {1}”,param.Item1,param.Item2);
if(i!= queryParams.Length - 1)
{
message.Append(“&”);
}
}
message.AppendLine();
message.AppendLine(随机数);
message.AppendLine(日期);
byte [] messageBuffer = Encoding.UTF8.GetBytes(message.ToString());
byte [] key = Convert.FromBase64String(m_securityKey);
byte [] computedHash;
使用(HMACSHA1 hmac = new HMACSHA1(key))
{
calculatedHash = hmac.ComputeHash(messageBuffer);
}
string hash = Convert.ToBase64String(calculatedHash);
request.AddHeader(“Authorization”,string.Format(“{0}:{1}”,nonce,hash));
退货要求;
}
通过自定义客户端,你可以得到有关API的URL(比如http://本地主机:8080 / vrageremote / API)可用资源的信息。