首页 > 解决方案 > C# Rest API 错误:Bitstamp 的“缺少密钥、签名和随机数参数”。我确实将它们包含在代码中..可能有什么问题?

问题描述

所以标题几乎说明了一切。我正在尝试向此 API 发出 POST 请求:https ://www.bitstamp.net/api/v2/balance/

我还按照此处的说明添加了所有名为 X-Auth 的正确标头:https ://www.bitstamp.net/api/

但是当我随后执行代码时,它告诉我我缺少密钥、签名和随机数参数。当我调试时,我确实在列表中看到了它们。所以我真的不明白为什么我仍然收到这个错误。有人可以查看代码并帮助我吗?

亲切的问候!这是代码:

using RestSharp;
using System;
using System.Security.Cryptography;
using System.Text;

namespace ConsoleApp5
{
    class Program
    {
        private readonly String _clientId = "xxx";
        private readonly String _apiKey = "xxx";
        private readonly String _apiSecret = "xxx";

        static void Main()
        {
            Program program = new Program();

            RestRequest request = new RestRequest("/api/v2/balance/", Method.POST);

            program.AddApiAuthentication(request);

            Console.ReadLine();
        }

        public void AddApiAuthentication(RestRequest restRequest)
        {
            var nonce = DateTime.Now.Ticks;
            var signature = GetSignature(nonce, _apiKey, _apiSecret, _clientId);
            long time = DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond;
            string version = "v2";
            string contentType = "application/x-www-form-urlencoded";

            restRequest.AddParameter("X-Auth", _apiKey);
            restRequest.AddParameter("X-Auth-Signature", signature);
            restRequest.AddParameter("X-Auth-Nonce", nonce);
            restRequest.AddParameter("X-Auth-Timestamp", time);
            restRequest.AddParameter("X-Auth-Version", version);
            restRequest.AddParameter("Content-Type", contentType);


            RestClient client = new RestClient
            {
                BaseUrl = new Uri("https://www.bitstamp.net/")
            };

            IRestResponse response = client.Execute(restRequest);
            Console.WriteLine(response.Content);
        }

        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, StringToByteArray(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[] StringToByteArray(string str)
        {
            return System.Text.Encoding.ASCII.GetBytes(str);
        }

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

标签: c#apiauthenticationnonce

解决方案


根据 Lasse V. Karlsen 告诉我的内容,我可以让它工作,这样它就不再只显示 API0000 错误。我开始遇到其他错误,并且在 C# Discord 频道上一个好人的帮助下,我可以找到解决方案。我现在在这里有一个完整的工作代码,它允许您使用您的 API 密钥进行私有 API 调用。这是代码:


using RestSharp;
using System;
using System.Security.Cryptography;
using System.Text;

namespace ConsoleApp5
{
    class Program
    {

        private readonly string _apiKey = "BITSTAMP" + " " + "XXX";
        private readonly string _apiSecret = "XXX";
        private readonly string _URL = "www.bitstamp.net/api/v2/balance/";
        private readonly string _queryParam = "";
        private readonly string _contentType = "";
        private readonly string _payloadString = "";

        static void Main()
        {
            Program program = new Program();

            RestRequest request = new RestRequest("api/v2/balance/", Method.POST);

            program.AddApiAuthentication(request);

            Console.ReadLine();
        }

        public void AddApiAuthentication(RestRequest restRequest)
        {
            var nonce = Guid.NewGuid();
            var timer = (long)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds);
            Console.WriteLine(timer);
            string version = "v2";
            var signature = GetSignature(nonce, _apiKey, _apiSecret, timer);
            restRequest.AddHeader("X-Auth", _apiKey);
            restRequest.AddHeader("X-Auth-Signature", signature);
            restRequest.AddHeader("X-Auth-Nonce", nonce.ToString()); ;
            restRequest.AddHeader("X-Auth-Timestamp", timer.ToString()); ;
            restRequest.AddHeader("X-Auth-Version", version);


            RestClient client = new RestClient
            {
                BaseUrl = new Uri("https://www.bitstamp.net/")
            };

            IRestResponse response = client.Execute(restRequest);
            Console.WriteLine(response.Content);
        }

        private string GetSignature(Guid nonce, string key, string secret, long timer)
        {
            string msg = $"{key}POST{_URL}{_queryParam}{_contentType}{nonce}{timer}v2{_payloadString}";
            Console.WriteLine(msg);

            return ByteArrayToString(SignHMACSHA256(secret, StringToByteArray(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[] StringToByteArray(string str)
        {
            return System.Text.Encoding.ASCII.GetBytes(str);
        }

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

只需将 XXX 更改为您自己的 API 密钥和 API 密码,将 URL 更改为您希望访问的任何 API,您就可以开始了!

我希望它有所帮助。耐西根。


推荐阅读