How do I, given the following methods, send the request to the Bitstamp API using RestSharp?

守給你的承諾、 提交于 2020-06-29 03:38:07

问题


I am trying to figure out as to how I can successfully retrieve my Bitcoin balance from Bitstamp using their API. I have spent the entire day on Stack Overflow and on youtube to try and figure this out, as there are a lot of small bits that could be molded together. I think that I'm pretty close to succeeding, but there is one small part that I can't seem to figure out.

How do I exactly execute this request? I should probably add the API authentication somewhere and then sign it using the HMACSHA256 method. But in what order? How do I go about it? This is the API documentation -> https://www.bitstamp.net/api/

And here is my entire code so far:

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(string[] args)
        {
            Console.ReadLine();
        }

        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, 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();
        }
    }
}

It's really just the executing part that I'm missing. I should've covered everything else according to the API documentation. Some thoughts would be great.

Thanks in advance, Nexigen.

EDIT::

I worked a little more on it and I now know how to execute it

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(string[] args)
        {
            Program program = new Program();

            var _request = new RestRequest();
            _request.Resource = "api/v2/balance";

            program.AddApiAuthentication(_request);

            Console.ReadLine();
        }

        public void AddApiAuthentication(RestRequest restRequest)
        {
            var nonce = DateTime.Now.Ticks;
            var signature = GetSignature(nonce, _apiKey, _apiSecret, _clientId);

            restRequest.AddParameter("X-Auth", _apiKey);
            restRequest.AddParameter("X-Auth-Signature", signature);
            restRequest.AddParameter("X-Auth-Nonce", nonce);

            var client = new RestClient();
            client.BaseUrl = new Uri("http://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();
        }
    }
}

However, when I now try to make the request to http://www.bitstamp.net/api/balance, It's telling me that it's missing my api key, signature and nonce parameters. And when I use http://www.bitstamp.net/api/v2/balance it's telling me that it's a POST endpoint only. What am I missing now?


回答1:


There are a couple of problems here:

  • you need to create a RestClient and a RestRequest and pass the request to the client after populating its headers
  • the header names you are using don't match those quoted in the bitstamp api documentation and the examples: enter image description here



回答2:


I pretty much found it out myself, by using the following code:


        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);
        }

You first make a new RestRequest and give it the POST method, then (in my case) you start the AddApiAuthentication and give it the request that you just made as a parameter. Then inside AddApiAuthentication you create a new RestClient, give it an URI and execute the call. Then you catch the response.

My question is answered, but I already have a new question based on an error I get after that. I'll be making a new question.



来源:https://stackoverflow.com/questions/62228010/how-do-i-given-the-following-methods-send-the-request-to-the-bitstamp-api-usin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!