我正在尝试将机器人写入Kucoin API,但创建签名失败了。
var input = nonce;
var stringToSign = "/user/info/"+input+"/";
byte[] secretkeyBytes = Encoding.UTF8.GetBytes(SecretKey);
byte[] inputBytes = Encoding.UTF8.GetBytes(stringToSign);
using (var hmac = new HMACSHA256(secretkeyBytes))
{
byte[] hashValue = hmac.ComputeHash(inputBytes);
return BitConverter.ToString(hashValue).Replace("-", "").ToLower();
}
但所有时间都会出错:
{“code”:“UNAUTH”,“msg”:“签名验证 失败”, “成功”:假, “时间戳”:1}
在官方网站上我们有这样的例子
String strForSign = endpoint + "/" + nonce +"/" + queryString;
//Make a base64 encoding of the completed string
String signatureStr = Base64.getEncoder().encodeToString(strForSign.getBytes("UTF-8"));
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secretKeySpec = new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256");
sha256_HMAC.init(secretKeySpec);
//KC-API-SIGNATURE in header
String signatureResult = Hex.encodeHexString(sha256_HMAC.doFinal(signatureStr.getBytes("UTF-8")));
我的代码出了什么问题?
答案 0 :(得分:3)
您错过了输入数据的base64编码步骤。你需要这样的东西:
// String interpolation just to be simpler; no need for input variable either
var stringToSign = $"/user/info/{nonce}/";
// This is the step you were missing
var signatureString = Convert.ToBase64String(Encoding.UTF8.GetBytes(stringToSign));
byte[] secretKeyBytes = Encoding.UTF8.GetBytes(SecretKey);
byte[] inputBytes = Encoding.UTF8.GetBytes(signatureString);
using (var hmac = new HMACSHA256(secretKeyBytes))
{
byte[] hashValue = hmac.ComputeHash(inputBytes);
return BitConverter.ToString(hashValue).Replace("-", "").ToLower();
}
答案 1 :(得分:1)
Daisy Shipton和Zimsan - 非常感谢,我和Kucoin有同样的问题,你的信息对我来说非常有用!我是StackOverflow的新手,我的投票和声誉太小,无法在网站上帮助你,但无论如何我点击了箭头。 )))
我想发布我的代码,它可以工作并包含正确的参数,因为Zimsan的代码包含带参数的小错误。
public KuCoin_Balance GetAccountBalance()
{
KuCoin_Balance tmpBalance = new KuCoin_Balance();
string domain = "https://api.kucoin.com";
string endpoint = "/v1/user/info";
string signatureResult = "";
string SecretKey = apiSecret;
long nonce = GetNonce();
var stringToSign = $"/v1/user/info/{nonce}/";
var signatureString = Convert.ToBase64String(Encoding.UTF8.GetBytes(stringToSign));
byte[] secretKeyBytes = Encoding.UTF8.GetBytes(SecretKey);
byte[] inputBytes = Encoding.UTF8.GetBytes(signatureString);
using (var hmac = new HMACSHA256(secretKeyBytes))
{
byte[] hashValue = hmac.ComputeHash(inputBytes);
signatureResult = BitConverter.ToString(hashValue).Replace("-", "").ToLower();
}
KuCoin_Response Resp = new KuCoin_Response();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(domain + endpoint);
request.Method = "GET";
request.Headers["KC-API-KEY"] = apiKey;
request.Headers["KC-API-NONCE"] = nonce.ToString();
request.Headers["KC-API-SIGNATURE"] = signatureResult; // apiSecret;
HttpWebResponse response = null;
try
{
response = (HttpWebResponse)request.GetResponse();
using (Stream responseStream = response.GetResponseStream())
{
if (responseStream != null)
{
using (StreamReader reader = new StreamReader(responseStream))
{
Resp.StatusCode = "1";
Resp.StatusDescription = "Success";
Resp.ResponseString = reader.ReadToEnd();
//return Resp;
}//End of StreamReader
}
}// End of using ResponseStream
}
catch (Exception ex)
{
Resp.StatusCode = "-1";
Resp.StatusDescription = "Error";
Resp.ResponseString = ex.Message.ToString() + " " + response.StatusDescription;
//return Resp;
}
return tmpBalance;
}
所以,非常感谢你,我花了不止一天才找到你的代码,现在我可以继续。我希望你能满足你生活中需要的支持!祝好运! Alex Vysotsky。