gpt4 book ai didi

c# - 调用 API : How to provide these parameters (key, 随机数和签名)

转载 作者:太空狗 更新时间:2023-10-29 21:44:59 26 4
gpt4 key购买 nike

我正在尝试实现一个 API。通常这很简单,但是这个给我带来了问题。 但是,我很确定问题出在我身上,而不是 API。

此特定 API 的网址:

https://www.bitstamp.net/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)));
}

知道为什么我会收到“缺少 key ”错误吗?有什么明显的我做错了吗(可能是:))?

编辑:

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 is a HMAC-SHA256 encoded message containing: nonce, client ID and API key. The HMAC-SHA256 code must be generated using a secret key that was generated with your API key. This code must be converted to it's hexadecimal representation (64 uppercase characters).

所以你必须将字节数组转换为十六进制字符串。看这个question获得一些如何做的例子。

关于c# - 调用 API : How to provide these parameters (key, 随机数和签名),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19481417/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com