使用 RestSharp 库获取 Coinbase 钱包列表
Get Coinbase wallet list with RestSharp library
我需要检索 Coinbase 帐户的钱包列表。为此,我需要使用 RestSharp(不允许使用第三个库),使用 API 私钥。
我试图检索它们,但是当我 运行 代码作为响应时,我得到了一个无效的响应,并显示一条错误消息
"The URI prefix is not recognized."
如何检索钱包列表?
这是我的代码:
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.
您将 scope 设置为路径,这是不正确的。
你应该打 GET https://api.coinbase.com/v2/accounts
参见:https://developers.coinbase.com/api/v2#list-accounts
路径应该是 "accounts",而不是 wallet:accounts:read
。
可以找到有关作用域的文档 here。
我需要检索 Coinbase 帐户的钱包列表。为此,我需要使用 RestSharp(不允许使用第三个库),使用 API 私钥。
我试图检索它们,但是当我 运行 代码作为响应时,我得到了一个无效的响应,并显示一条错误消息
"The URI prefix is not recognized."
如何检索钱包列表?
这是我的代码:
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.
您将 scope 设置为路径,这是不正确的。
你应该打 GET https://api.coinbase.com/v2/accounts
参见:https://developers.coinbase.com/api/v2#list-accounts
路径应该是 "accounts",而不是 wallet:accounts:read
。
可以找到有关作用域的文档 here。