[英]C# Rest API error: “Missing key, signature and nonce parameters” for Bitstamp. I did include them in the code.. what could be wrong?
[英]Calling API: How to provide these parameters (key, nonce and signature)
我正在玩實現API。 通常這很簡單,但這個給我帶來了麻煩。 但是,我很確定問題是我 ,而不是API。
此特定API的網址:
我想對“帳戶余額”進行POST調用。 目前我得到以下答案:
{"error": "Missing key, signature and nonce parameters"}
我嘗試使用以下代碼:
var path = "https://www.bitstamp.net/api/user_transactions";
var nonce = GetNonce();
var signature = GetSignature(nonce);
using (WebClient client = new WebClient())
{
byte[] response = client.UploadValues(path, new NameValueCollection()
{
{ "key", Constants.ThirdParty.BitStamp.ApiKey },
{ "signature", signature },
{ "nonce", nonce},
});
var str = System.Text.Encoding.Default.GetString(response);
}
return 0.0m;
這是我的兩個輔助函數:
private string GetSignature(int nonce)
{
string msg = string.Format("{0}{1}{2}", nonce,
Constants.ThirdParty.BitStamp.ClientId,
Constants.ThirdParty.BitStamp.ApiKey);
return HelperFunctions.sign(Constants.ThirdParty.BitStamp.ApiSecret, msg);
}
public static int GetNonce()
{
return (int) (DateTime.Now - new DateTime(1970, 1, 1)).TotalSeconds;
}
我的加密簽名功能就是這個:
public static String sign(String key, String stringToSign)
{
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] keyByte = encoding.GetBytes(key);
HMACSHA256 hmacsha256 = new HMACSHA256(keyByte);
return Convert.ToBase64String(hmacsha256.ComputeHash(encoding.GetBytes(stringToSign)));
}
知道為什么我得到“丟失密鑰”錯誤? 有什么明顯的東西我做錯了(可能是:))?
編輯 :
Fiddler告訴我發布以下數據:
key=mykeymykey&signature=PwhdkSek6GP%2br%2bdd%2bS5aU1MryXgrfOD4fLH05D7%2fRLQ%3d&nonce=1382299103%2c21055
編輯#2 :
更新了有關生成簽名的代碼:
private string GetSignature(int nonce)
{
string msg = string.Format("{0}{1}{2}", nonce,
Constants.ThirdParty.BitStamp.ClientId,
Constants.ThirdParty.BitStamp.ApiKey);
return HelperFunctions.ByteArrayToString(HelperFunctions.SignHMACSHA256(
Constants.ThirdParty.BitStamp.ApiSecret, msg)).ToUpper();
}
public static byte[] SignHMACSHA256(String key, byte[] data)
{
HMACSHA256 hashMaker = new HMACSHA256(Encoding.ASCII.GetBytes(key));
return hashMaker.ComputeHash(data);
}
public static byte[] StrinToByteArray(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
public static string ByteArrayToString(byte[] ba)
{
return BitConverter.ToString(ba).Replace("-", "");
}
我認為問題在於您使用的是base64( Convert.ToBase64String
),但在API文檔的相關部分中,它是:
Signature是HMAC-SHA256編碼消息,包含:nonce,客戶端ID和API密鑰。 必須使用通過API密鑰生成的密鑰生成HMAC-SHA256代碼。 必須將此代碼轉換為十六進制表示形式(64個大寫字符)。
所以你必須將字節數組轉換為十六進制字符串。 請參閱此問題以獲取有關如何執行此操作的示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.