Advertisement
Nex92

C# API call Bitstamp, still not working

Jun 6th, 2020
1,100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.06 KB | None | 0 0
  1. using RestSharp;
  2. using System;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5.  
  6. namespace ConsoleApp5
  7. {
  8.     class Program
  9.     {
  10.         private readonly String _clientId = "xxx";
  11.         private readonly String _apiKey = "xxx";
  12.         private readonly String _apiSecret = "xxx";
  13.  
  14.         static void Main(string[] args)
  15.         {
  16.             Program program = new Program();
  17.  
  18.             var _request = new RestRequest();
  19.             _request.Resource = "api/v2/balance";
  20.  
  21.             program.AddApiAuthentication(_request);
  22.  
  23.             Console.ReadLine();
  24.         }
  25.  
  26.         public void AddApiAuthentication(RestRequest restRequest)
  27.         {
  28.             var nonce = DateTime.Now.Ticks;
  29.             var signature = GetSignature(nonce, _apiKey, _apiSecret, _clientId);
  30.  
  31.             restRequest.AddParameter("X-Auth", _apiKey);
  32.             restRequest.AddParameter("X-Auth-Signature", signature);
  33.             restRequest.AddParameter("X-Auth-Nonce", nonce);
  34.  
  35.             var client = new RestClient();
  36.             client.BaseUrl = new Uri("http://www.bitstamp.net/");
  37.  
  38.             IRestResponse response = client.Execute(restRequest);
  39.             Console.WriteLine(response.Content);
  40.         }
  41.  
  42.         private string GetSignature(long nonce, string key, string secret, string clientId)
  43.         {
  44.             string msg = string.Format("{0}{1}{2}", nonce,
  45.                 clientId,
  46.                 key);
  47.  
  48.             return ByteArrayToString(SignHMACSHA256(secret, StringToByteArray(msg))).ToUpper();
  49.         }
  50.         public static byte[] SignHMACSHA256(String key, byte[] data)
  51.         {
  52.             HMACSHA256 hashMaker = new HMACSHA256(Encoding.ASCII.GetBytes(key));
  53.             return hashMaker.ComputeHash(data);
  54.         }
  55.  
  56.         public static byte[] StringToByteArray(string str)
  57.         {
  58.             return System.Text.Encoding.ASCII.GetBytes(str);
  59.         }
  60.  
  61.         public static string ByteArrayToString(byte[] hash)
  62.         {
  63.             return BitConverter.ToString(hash).Replace("-", "").ToLower();
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement