Get Coinbase wallet list with RestSharp library

2019-08-28 13:02发布

I need to retrieve the list of wallets of a Coinbase account. In order to to it, I need to use RestSharp (no third library allowed), using the API private keys.

I've tried to retrieve them but when I run the code, as response I obtain a invalid response, with an error message that says

"The URI prefix is not recognized."

How can I retrieve the list of wallets?

This is my code:

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条回答
老娘就宠你
2楼-- · 2019-08-28 13:30

The reason you're getting this error is the URL is composed as https://api.coinbase.com/v2/wallet:accounts:read, which isn't a valid URL.

You're setting the scope as the path, which is incorrect.

You should be hitting GET https://api.coinbase.com/v2/accounts

See: https://developers.coinbase.com/api/v2#list-accounts

Path should be "accounts", not wallet:accounts:read.

Documentation on scopes can be found here.

查看更多
登录 后发表回答