给定以下方法,我如何使用 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/>
这是我到目前为止的全部代码:
使用RestSharp;使用系统;使用 System.Security.Cryptography;使用 System.Text;命名空间 ConsoleApp5{课程计划{私有只读字符串_clientId =xxx";私有只读字符串_apiKey =xxx";私有只读字符串_apiSecret =xxx";static void Main(string[] args){Console.ReadLine();}public void AddApiAuthentication(RestRequest restRequest){var nonce = DateTime.Now.Ticks;var 签名 = GetSignature(nonce, _apiKey, _apiSecret, _clientId);restRequest.AddParameter("key", _apiKey);restRequest.AddParameter("签名", 签名);restRequest.AddParameter("nonce", nonce);}私有字符串 GetSignature(long nonce, string key, string secret, string clientId){string msg = string.Format("{0}{1}{2}", nonce,客户 ID,钥匙);返回 ByteArrayToString(SignHMACSHA256(secret, StringToByteArray(msg))).ToUpper();}public static byte[] SignHMACSHA256(String key, byte[] data){HMACSHA256 hashMaker = new HMACSHA256(Encoding.ASCII.GetBytes(key));返回 hashMaker.ComputeHash(data);}public static byte[] StringToByteArray(string str){返回 System.Text.Encoding.ASCII.GetBytes(str);}公共静态字符串 ByteArrayToString(byte[] hash){返回 BitConverter.ToString(hash).Replace("-", "").ToLower();}}}
这真的只是我缺少的执行部分.根据 API 文档,我应该涵盖所有其他内容.有些想法会很棒.
提前致谢,Nexigen.
:
我做了更多的工作,现在我知道如何执行它
使用RestSharp;使用系统;使用 System.Security.Cryptography;使用 System.Text;命名空间 ConsoleApp5{课程计划{私有只读字符串_clientId =xxx";私有只读字符串_apiKey =xxx";私有只读字符串_apiSecret =xxx";static void Main(string[] args){程序程序=新程序();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 签名 = GetSignature(nonce, _apiKey, _apiSecret, _clientId);restRequest.AddParameter("X-Auth", _apiKey);restRequest.AddParameter("X-Auth-Signature", 签名);restRequest.AddParameter("X-Auth-Nonce", nonce);var client = new RestClient();client.BaseUrl = new Uri("http://www.bitstamp.net/");IRestResponse 响应 = client.Execute(restRequest);Console.WriteLine(response.Content);}私有字符串 GetSignature(long nonce, string key, string secret, string clientId){string msg = string.Format("{0}{1}{2}", nonce,客户 ID,钥匙);返回 ByteArrayToString(SignHMACSHA256(secret, StringToByteArray(msg))).ToUpper();}public static byte[] SignHMACSHA256(String key, byte[] data){HMACSHA256 hashMaker = new HMACSHA256(Encoding.ASCII.GetBytes(key));返回 hashMaker.ComputeHash(data);}public static byte[] StringToByteArray(string str){返回 System.Text.Encoding.ASCII.GetBytes(str);}公共静态字符串 ByteArrayToString(byte[] hash){返回 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屋!