我一直在寻找一个好的解决方案,我从邮递员那里复制了 API 中使用的代码,并尝试在 Xamarin 表单中使用它。问题是 API 中有一个生成“签名”的方法,它们在 JS 中执行。我尝试了各种解决方案,但它不会生成相同的消息。
在 JS =
(function () {
var timestamp = getTime();
pm.environment.set("timestamp",timestamp);
var clientId = pm.environment.get("client_id");
var secret = pm.environment.get("secret");
var sign = calcSign(clientId,secret,timestamp);
pm.environment.set('easy_sign', sign);
})();
function getTime(){
var timestamp = new Date().getTime();
return timestamp;
}
function calcSign(clientId,secret,timestamp){
var str = clientId + timestamp;
var hash = CryptoJS.HmacSHA256(str, secret);
var hashInBase64 = hash.toString();
var signUp = hashInBase64.toUpperCase();
return signUp;
}
在 C# =
public void timeStamp()
{
/*
* var str = clientId + timestamp;
var hash = CryptoJS.HmacSHA256(str, secret);
var hashInBase64 = hash.toString();
var signUp = hashInBase64.toUpperCase();
return signUp;
*/
var timestamp = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
var t = (DateTime.Now.ToUniversalTime() - timestamp);
time = t.ToString();
//CalcSign
var str = clientID + t;
var key = Convert.FromBase64String(secret);
//var key = Convert.FromBase64String(str);
Console.Write("key:");
prtByte(key);
var provider = new System.Security.Cryptography.HMACSHA256(key);
var hash = provider.ComputeHash(Encoding.UTF8.GetBytes(str));
Console.Write("hash:");
prtByte(hash);
var signature = Convert.ToBase64String(hash);
Console.WriteLine("signature:" + signature);
var signUp = signature.ToUpper();
sign = signUp;
}
public static void prtByte(byte[] b)
{
for (var i = 0; i < b.Length; i++)
{
Console.Write(b[i].ToString("x2"));
}
Console.WriteLine();
}
我得到的消息是=“请求时间无效”。