我需要检索 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
.
可以在此处找到有关范围的文档。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句