使用RestSharp库获取Coinbase钱包列表

时间:2017-12-15 15:33:05

标签: c# restsharp coinbase-api

我需要检索Coinbase帐户的钱包列表。为了实现它,我需要使用RestSharp(不允许第三个库),使用API​​私钥。

我试图检索它们但是当我运行代码时,作为响应我得到了无效的响应,并显示错误消息

  

"无法识别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;
      }
    }
  }
}

1 个答案:

答案 0 :(得分:2)

您收到此错误的原因是网址由https://api.coinbase.com/v2/wallet:accounts:read组成,而且不是有效的网址。

您将范围设置为路径,这是不正确的。

你应该点击GET https://api.coinbase.com/v2/accounts

请参阅:https://developers.coinbase.com/api/v2#list-accounts

路径应该是"帐户"而不是wallet:accounts:read

可以找到有关范围的文档here