昨天写了一篇文章《
Bigone API 升级到v2,害死程序员
》,有人反映API文档无法打开,请
自备梯子
访问https://open.big.one。
文档中明确规定了API的访问限制:
昨天的例子中的Ping是公开访问的API,即不需要API token即可访问,而更多的涉及到账户查询、订单查询等操作是私有API,需要用到上一篇文章中提到的Header来访问API网址。
对于C#获取https URL的返回内容,可以参考以下代码:
public static string GetUrl(string url, string[] headers = null)
{
ServicePointManager.Expect100Continue = true;
// 如果考虑兼容性,用SecurityProtocolType.Ssl3
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.UserAgent = DEFAULT_USER_AGENT;
request.Method = "GET";
request.KeepAlive = false;
if (headers != null)
{
foreach (string h in headers)
{
request.Headers.Add(h);
}
}
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
using (System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
string srcString = reader.ReadToEnd();
return srcString;
}
}
}
访问一个bigone账户的余额的API为:
https://b1.run/api/v2/viewer/accounts
如果一切正常,则返回类似的内容:
"locked_balance":"0.111",
"balance":"0.765",
"asset_uuid":"c98f5d90-c619-4de2-b643-3d429f622239",
"asset_id":"ETH"
取出所有数字资产的代码就非常容易了,写一个Asset类,再加一段代码:
public static Dictionary
ParseBigoneAssetJson(string json)
{
Dictionary
dict = new Dictionary
();
var root = JObject.Parse(json);
JArray data = root["data"] as JArray;
foreach (JToken token in data)
{
Asset a = new Asset();
a.ID = token["asset_id"].ToString();
a.LockedBalance = double.Parse(token["locked_balance"].ToString());
a.Balance = double.Parse(token["balance"].ToString());
if (a.Balance > 0.001)
dict.Add(a.ID, a);
}
return dict;
}
币价跌得太难看,我就不贴出我的bigone余额啦。
点击文末的广告,我会收到5毛钱,
用于支付北京呼家楼6800元的房租,谢谢!
--- END ---
近期文章: