给定以下方法,我如何使用RestSharp将请求发送到Bitstamp API? [英] How do I, given the following methods, send the request to the Bitstamp API using RestSharp?
问题描述
我试图弄清楚如何使用他们的API从Bitstamp成功检索我的比特币余额.我花了整整一天时间在Stack Overflow和youtube上尝试解决这个问题,因为可以将许多小的部分整合在一起.我认为我已经接近成功了,但其中有一小部分我似乎无法弄清楚.
如何准确执行此请求?我可能应该在某个地方添加API身份验证,然后使用HMACSHA256方法对其进行签名.但是以什么顺序?我该怎么办? 这是API文档-> 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(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();
}
}
}
实际上我只是缺少执行部分.根据API文档,我应该介绍所有其他内容.有些想法会很棒.
预先感谢, Nexigen.
:
我做了更多的工作,现在我知道如何执行
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();
}
}
}
但是,当我现在尝试向 http://www.bitstamp.net/发出请求时api/balance ,它告诉我它缺少我的api密钥,签名和随机数参数.当我使用 http://www.bitstamp.net/api/v2/balance告诉我这只是一个POST端点.我现在想念什么?
这里有两个问题:
- 您需要创建一个
RestClient
和RestRequest
,并在填充其标头之后将请求传递给客户端 - 您使用的标头名称与bitstamp api文档和示例中引用的标头名称不匹配:
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?
There are a couple of problems here:
- you need to create a
RestClient
and aRestRequest
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:
这篇关于给定以下方法,我如何使用RestSharp将请求发送到Bitstamp API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!