调用API:如何提供这些参数(键,随机数和签名)

Lar*_*ard 6 c# api post c#-4.0

我正在玩实现API.通常这很简单,但这个给我带来了麻烦.但是,我很确定问题是我,而不是API.

此特定API的网址:

https://www.bitstamp.net/api/

我想对"帐户余额"进行POST调用.目前我得到以下答案:

{"error": "Missing key, signature and nonce parameters"}
Run Code Online (Sandbox Code Playgroud)

我尝试使用以下代码执行此操作:

        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;
Run Code Online (Sandbox Code Playgroud)

这是我的两个辅助函数:

    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;
    }
Run Code Online (Sandbox Code Playgroud)

我的加密签名功能就是这个:

    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)));
    }
Run Code Online (Sandbox Code Playgroud)

知道为什么我得到"丢失密钥"错误?有什么明显的东西我做错了(可能是:))?

编辑:

Fiddler告诉我发布以下数据:

key=mykeymykey&signature=PwhdkSek6GP%2br%2bdd%2bS5aU1MryXgrfOD4fLH05D7%2fRLQ%3d&nonce=1382299103%2c21055
Run Code Online (Sandbox Code Playgroud)

编辑#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("-", "");
}
Run Code Online (Sandbox Code Playgroud)

ast*_*ada 4

我认为问题在于您使用的是base64(Convert.ToBase64String),但在API文档的相关部分中,它是这样写的:

签名是一条 HMAC-SHA256 编码消息,包含:随机数、客户端 ID 和 API 密钥。必须使用通过 API 密钥生成的密钥来生成 HMAC-SHA256 代码。该代码必须转换为其十六进制表示形式(64 个大写字符)。

所以你必须将字节数组转换为十六进制字符串。请参阅此问题以获取有关如何执行此操作的一些示例。

  • 我测试了一下,发现问题出在你使用的URL上。如果您向“https://www.bitstamp.net/api/user_transactions”发送 POST,您将被重定向到“https://www.bitstamp.net/api/user_transactions/”(注意最后的斜杠)。`WebClient` 自动遵循重定向,但不会重新上传值。您应该尝试 POST 到以斜杠结尾的 URL。 (4认同)