使用 RestSharp 库获取 Coinbase 钱包列表 [英] Get Coinbase wallet list with RestSharp library

查看:53
本文介绍了使用 RestSharp 库获取 Coinbase 钱包列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检索 Coinbase 帐户的钱包列表.为此,我需要使用 RestSharp(不允许使用第三个库),使用 API 私钥.

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

无法识别 URI 前缀."

"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:读取,这不是一个有效的网址.

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.

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

参见:https://developers.coinbase.com/api/v2#list- 帐户

路径应该是accounts",而不是wallet:accounts:read.

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

可以在此处找到有关范围的文档.

Documentation on scopes can be found here.

这篇关于使用 RestSharp 库获取 Coinbase 钱包列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆