Advertisement
Nex92

C# API call Bitstamp, still not working..

Jun 6th, 2020
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1.  
  2. using RestSharp;
  3. using System;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6.  
  7. namespace ConsoleApp5
  8. {
  9. class Program
  10. {
  11. private readonly String _clientId = "xxx";
  12. private readonly String _apiKey = "xxx";
  13. private readonly String _apiSecret = "xxx";
  14.  
  15. static void Main()
  16. {
  17. Program program = new Program();
  18.  
  19. RestRequest request = new RestRequest("/api/v2/balance/", Method.POST);
  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. long time = DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond;
  31. string version = "v2";
  32. string contentType = "application/x-www-form-urlencoded";
  33.  
  34. restRequest.AddParameter("X-Auth", _apiKey);
  35. restRequest.AddParameter("X-Auth-Signature", signature);
  36. restRequest.AddParameter("X-Auth-Nonce", nonce);
  37. restRequest.AddParameter("X-Auth-Timestamp", time);
  38. restRequest.AddParameter("X-Auth-Version", version);
  39. restRequest.AddParameter("Content-Type", contentType);
  40.  
  41.  
  42. RestClient client = new RestClient
  43. {
  44. BaseUrl = new Uri("https://www.bitstamp.net/")
  45. };
  46.  
  47. IRestResponse response = client.Execute(restRequest);
  48. Console.WriteLine(response.Content);
  49. }
  50.  
  51. private string GetSignature(long nonce, string key, string secret, string clientId)
  52. {
  53. string msg = string.Format("{0}{1}{2}", nonce,
  54. clientId,
  55. key);
  56.  
  57. return ByteArrayToString(SignHMACSHA256(secret, StringToByteArray(msg))).ToUpper();
  58. }
  59. public static byte[] SignHMACSHA256(String key, byte[] data)
  60. {
  61. HMACSHA256 hashMaker = new HMACSHA256(Encoding.ASCII.GetBytes(key));
  62. return hashMaker.ComputeHash(data);
  63. }
  64.  
  65. public static byte[] StringToByteArray(string str)
  66. {
  67. return System.Text.Encoding.ASCII.GetBytes(str);
  68. }
  69.  
  70. public static string ByteArrayToString(byte[] hash)
  71. {
  72. return BitConverter.ToString(hash).Replace("-", "").ToLower();
  73. }
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement