[英]Get Coinbase wallet list with RestSharp library
我需要检索一个Coinbase账户的钱包列表。 为此,我需要使用API私钥使用RestSharp(不允许使用第三个库)。
我尝试检索它们,但是当我运行代码时,作为响应,我获得了无效响应,并显示一条错误消息,内容为
“无法识别URI前缀。”
如何获取钱包列表?
这是我的代码:
using RestSharp;
using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace WCoinBase
{
class Program
{
private const string apiKey = "MyPrivateKey";
static void Main(string[] args)
{
RestClient restClient = new RestClient
{
BaseUrl = new Uri("https://api.coinbase.com/v2/")
};
string timestamp = DateTimeOffset.Now.ToUnixTimeSeconds().ToString();
string path = "wallet:accounts:read";
var request = new RestRequest
{
Method = Method.GET,
Resource = path
};
string accessSign = GetAccessSign(timestamp, "GET", path, "");
request.AddHeader("CB-ACCESS-KEY", apiKey);
request.AddHeader("CB-ACCESS-SIGN", accessSign);
request.AddHeader("CB-ACCESS-TIMESTAMP", timestamp);
request.AddHeader("CB-VERSION", "2017-08-07");
request.AddHeader("Accept", "application/json");
var response = restClient.Execute(request);
Console.WriteLine("Status Code: " + response.StatusCode);
}
static private string GetAccessSign(string timestamp, string command, string path, string body)
{
var hmacKey = Encoding.UTF8.GetBytes(apiKey);
string data = timestamp + command + path + body;
using (var signatureStream = new MemoryStream(Encoding.UTF8.GetBytes(data)))
{
var hex = new HMACSHA256(hmacKey).ComputeHash(signatureStream)
.Aggregate(new StringBuilder(), (sb, b) => sb.AppendFormat("{0:x2}", b), sb => sb.ToString());
return hex;
}
}
}
}
您收到此错误的原因是该URL由https://api.coinbase.com/v2/wallet:accounts:read
组成,它不是有效的URL。
您将范围设置为路径,这是不正确的。
您应该点击GET https://api.coinbase.com/v2/accounts
请参阅: https : //developers.coinbase.com/api/v2#list-accounts
路径应为“帐户”,而不是wallet:accounts:read
。
有关范围的文档可在此处找到。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.