c# - RestSharp BitStamp 身份验证失败

标签 c# api restsharp bitcoin

我无法从 BitStamp API 接收任何数据。 我在这里做错了什么?我的内容形成了错误的响应结果:

{"error": "Missing key, signature and nonce parameters"}

        public ActionResult Index()
        {
            const string BaseUrl = "https://www.bitstamp.net/api/balance/";
            var client = new RestClient();
            var request = new RestRequest();

            client.BaseUrl = BaseUrl;
            AddApiAuthentication(request);
            var response = client.Execute(request);
            var foo = response.Content;
            //{"error": "Missing key, signature and nonce parameters"}
            return View();
        }

        public void AddApiAuthentication(RestRequest restRequest)
        {
            var nonce = DateTime.Now.Ticks;
            var signature = GetSignature(nonce, apiKey, apiSecret, clientId);

            restRequest.AddParameter("key", apiKey);
            restRequest.AddParameter("signature", signature);
            restRequest.AddParameter("nonce", nonce);

        }

        private string GetSignature(long nonce, string key, string secret, string clientId)
        {
            string msg = string.Format("{0}{1}{2}", nonce, clientId, key);
            return ByteArrayToString(SignHMACSHA256(secret, StrinToByteArray(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)
        {
            return System.Text.Encoding.ASCII.GetBytes(str);
        }

        public static string ByteArrayToString(byte[] hash)
        {
            return BitConverter.ToString(hash).Replace("-", "").ToLower();
        }

API 身份验证似乎对 this post 中的用户有效. 我是否以正确的方式执行请求?

最佳答案

对于我们的新应用程序,我们使用了以下代码:

public class BitstampAuthenticatedRequest : RestRequest
{
    #region Data
    private readonly string _clientId = "xxxxx"; // Numbers

    private readonly string _apiKey = "xxxxx"; // Random numbers and letters
    private readonly string _apiSecret = "xxxx"; // Random numbers and letters

    private long Nonce = DateTime.Now.Ticks;

    #endregion

    #region Constructor

    public BitstampAuthenticatedRequest(string resource, Method method)
    : base(resource, method)
    {
        this.AddParameter("key", _apiKey);            
        this.AddParameter("nonce", Nonce);
        this.AddParameter("signature", CreateSignature());
    }

    #endregion

    #region Methods

    private string CreateSignature()
    {
        string msg = string.Format("{0}{1}{2}", Nonce,
            _clientId,
            _apiKey);

        return ByteArrayToString(SignHMACSHA256(_apiSecret, StringToByteArray(msg))).ToUpper();
    }

    private static byte[] SignHMACSHA256(String key, byte[] data)
    {
        HMACSHA256 hashMaker = new HMACSHA256(Encoding.ASCII.GetBytes(key));
        return hashMaker.ComputeHash(data);
    }

    private static byte[] StringToByteArray(string str)
    {
        return System.Text.Encoding.ASCII.GetBytes(str);
    }

    private static string ByteArrayToString(byte[] hash)
    {
        return BitConverter.ToString(hash).Replace("-", "").ToLower();
    }

    #endregion
}

然后这样调用它:

        // Decide which url to use
        var baseUrl = "https://www.bitstamp.net/api/balance/";

        // Create the authenticated request
        RestRequest request = new BitstampAuthenticatedRequest(baseUrl, Method.POST);

        // Get the response
        var response = new RestClient().Execute(request);

Justed 在控制台应用程序中再次测试了代码,并且可以正常工作。如果您需要进一步的帮助,请发表评论。

关于c# - RestSharp BitStamp 身份验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21612185/

相关文章:

api - 如何知道谁是(二级)Tumblr 博客的所有者?如何了解关注者博客?

restsharp - 为什么 RestSharp 在反序列化 bool 响应时会抛出错误?

windows-phone-7 - 取消 RestSharp 请求

c# - RestSharp 忽略响应字符集编码

c# - 如何在向父类构造函数传递参数时向子类构造函数添加更多代码?

c# - 在 Swagger for ASP .NET Core 3.1 中为标题添加过滤器

node.js - Module parse failed top-level-await 实验未启用

c# - 如何使用 EWS 托管 API 从文件夹和公共(public)文件夹的子文件夹中获取所有项目

c# - 如何将字符插入字母数字字符串

c# - 在食物成分文本中查找 "E numbers"的正则表达式