RestSharp BitStamp身份验证失败 [英] RestSharp BitStamp Authentication fails
本文介绍了RestSharp BitStamp身份验证失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不能够从BitStamp API接收任何数据。
我在做什么错在这里?我的内容形成一个错误的响应结果:
I'm not able to receive any data from the BitStamp API. What am I doing wrong here? My content form the response results in an error:
{"error": "Missing key, signature and nonce parameters"}
public ActionResult Index()
{
const string BaseUrl = "https://www.bitstamp.net/api/balance/";
var client = new RestClient();
var request = new RestRequest();
client.BaseUrl = BaseUrl;
AddApiAuthentication(request);
var response = client.Execute(request);
var foo = response.Content;
//{"error": "Missing key, signature and nonce parameters"}
return View();
}
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, StrinToByteArray(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[] StrinToByteArray(string str)
{
return System.Text.Encoding.ASCII.GetBytes(str);
}
public static string ByteArrayToString(byte[] hash)
{
return BitConverter.ToString(hash).Replace("-", "").ToLower();
}
该API认证,似乎在<一个用户工作href=\"http://stackoverflow.com/questions/19717095/bitstamp-new-authentication-in-c-sharp-signature\">this帖子。
我在执行以正确的方式请求?
The API authentication seems to work for the user in this post. Am I executing the request in the right way?
推荐答案
有关我们的新的应用程序,我们使用以下code:
For our new application we used the following code:
public class BitstampAuthenticatedRequest : RestRequest
{
#region Data
private readonly string _clientId = "xxxxx"; // Numbers
private readonly string _apiKey = "xxxxx"; // Random numbers and letters
private readonly string _apiSecret = "xxxx"; // Random numbers and letters
private long Nonce = DateTime.Now.Ticks;
#endregion
#region Constructor
public BitstampAuthenticatedRequest(string resource, Method method)
: base(resource, method)
{
this.AddParameter("key", _apiKey);
this.AddParameter("nonce", Nonce);
this.AddParameter("signature", CreateSignature());
}
#endregion
#region Methods
private string CreateSignature()
{
string msg = string.Format("{0}{1}{2}", Nonce,
_clientId,
_apiKey);
return ByteArrayToString(SignHMACSHA256(_apiSecret, StringToByteArray(msg))).ToUpper();
}
private static byte[] SignHMACSHA256(String key, byte[] data)
{
HMACSHA256 hashMaker = new HMACSHA256(Encoding.ASCII.GetBytes(key));
return hashMaker.ComputeHash(data);
}
private static byte[] StringToByteArray(string str)
{
return System.Text.Encoding.ASCII.GetBytes(str);
}
private static string ByteArrayToString(byte[] hash)
{
return BitConverter.ToString(hash).Replace("-", "").ToLower();
}
#endregion
}
和调用它:
// Decide which url to use
var baseUrl = "https://www.bitstamp.net/api/balance/";
// Create the authenticated request
RestRequest request = new BitstampAuthenticatedRequest(baseUrl, Method.POST);
// Get the response
var response = new RestClient().Execute(request);
在Justed控制台应用程序再次测试code和工作是肯定的。如果您需要进一步的帮助只是发表评论。
Justed tested the code again in console application and works for sure. If you need further help just comment.
这篇关于RestSharp BitStamp身份验证失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文