如何从 Coinbase 读取账户余额

How to read the account balance from Coinbase

我能够 运行 https://github.com/iYalovoy/demibyte.coinbase 提供的示例项目。 我不确定是否有办法直接读取总帐户余额,因为它列在 https://www.coinbase.com/dashboard(总余额)下。 下面的代码能够检索价格(实际上并不需要提供真实的 apiKey 和 apiSecret),但是当我尝试读取帐户时它会给出错误 401(未授权)。错误详细信息是:

{"errors":[{"id":"authentication_error","message":"invalid signature"}]}

我在 API 设置中启用了 "wallet:accounts:read"。

using System;
using System.Text;
using System.Security.Cryptography;
using Flurl;
using Flurl.Http;
using Newtonsoft.Json;
using System.Net;

namespace demibyte.coinbase
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            var host = "https://api.coinbase.com/";
            var apiKey = "my api key";
            var apiSecret = "my api secret";

            var unixTimestamp = (Int32)(DateTime.UtcNow.Subtract (new DateTime (1970, 1, 1))).TotalSeconds;
            var currency = "AUD";
            var apiVersion = "2016-03-03";  // check value from https://www.coinbase.com/settings/api - it is the build date of Coinbase API (not our Application!) 
            var message = string.Format ("{0}GET/v2/prices/spot?currency={1}", unixTimestamp.ToString (), currency);

            byte[] secretKey = Encoding.UTF8.GetBytes (apiSecret);
            HMACSHA256 hmac = new HMACSHA256 (secretKey);

            hmac.Initialize ();
            byte[] bytes = Encoding.UTF8.GetBytes (message);
            byte[] rawHmac = hmac.ComputeHash (bytes);
            var signature = rawHmac.ByteArrayToHexString ();

            var price = host
                .AppendPathSegment ("v2/prices/spot")
                .SetQueryParam ("currency", currency)
                .WithHeader ("CB-ACCESS-SIGN", signature)
                .WithHeader ("CB-ACCESS-TIMESTAMP", unixTimestamp)
                .WithHeader ("CB-ACCESS-KEY", apiKey)
                .WithHeader ("CB-VERSION", apiVersion)
                .GetJsonAsync<dynamic> ()
                .Result;

            Console.WriteLine (price.ToString (Formatting.None));
            Console.WriteLine();

            message = string.Format ("GET/v2/accounts");

            bytes = Encoding.UTF8.GetBytes (message);
            rawHmac = hmac.ComputeHash (bytes);
            signature = rawHmac.ByteArrayToHexString ();

            var value = host
                .AppendPathSegment ("v2/accounts")
                .SetQueryParam ("currency", currency)
                .WithHeader ("CB-ACCESS-SIGN", signature)
                .WithHeader ("CB-ACCESS-TIMESTAMP", unixTimestamp)
                .WithHeader ("CB-ACCESS-KEY", apiKey)
                .WithHeader ("CB-VERSION", apiVersion)
                .GetJsonAsync<dynamic> ()
                .Result;

            Console.WriteLine (value.ToString (Formatting.None));

            Console.ReadLine ();
       }
    }
}

没有总账户价值return。

您正在访问 /accounts 端点,它应该是 return 格式为 JSON 对象的分页列表。您必须解析每个元素以找到您要查找的内容,做一些数学运算,然后创建一个 运行 总数。

无论如何,您的错误是无关的,甚至与服务身份验证有关。

这段代码工作正常。我将 API 升级到最新版本(日期是 2018-01-13)。我还删除了所有 API 键,并为多种货币创建了一个。我只能在开始时看到 API 秘密,当我创建新密钥时 - 后来它不再显示了。我注意到偶尔 api 表明我们没有发送 CB 版本,我不确定为什么会这样。

using System;
using System.Text;
using System.Security.Cryptography;
using Flurl;
using Flurl.Http;
using Newtonsoft.Json;
using System.Linq;

namespace demibyte.coinbase
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            var host = "https://api.coinbase.com/";
            var apiKey = "myApiKey";
            var apiSecret = "myApiSecret";

            var unixTimestamp = (Int32)(DateTime.UtcNow.Subtract (new DateTime (1970, 1, 1))).TotalSeconds;
            var currency = "AUD";
            var apiVersion = "2018-01-13";  // check value from https://www.coinbase.com/settings/api - it is the build date of Coinbase API (not our Application!) 
            var message = string.Format ("{0}GET/v2/prices", unixTimestamp.ToString ());

            byte[] secretKey = Encoding.UTF8.GetBytes (apiSecret);
            HMACSHA256 hmac = new HMACSHA256 (secretKey);

            hmac.Initialize ();
            byte[] bytes = Encoding.UTF8.GetBytes (message);
            byte[] rawHmac = hmac.ComputeHash (bytes);
            var signature = rawHmac.ByteArrayToHexString ();

            var jsonCodeBTC = host
                .AppendPathSegment ("v2/prices/BTC-AUD/spot")
                .WithHeader ("CB-VERSION", apiVersion)                                          
                .WithHeader ("CB-ACCESS-SIGN", signature)
                .WithHeader ("CB-ACCESS-TIMESTAMP", unixTimestamp)
                .WithHeader ("CB-ACCESS-KEY", apiKey)
                .GetJsonAsync<dynamic> ()
                .Result;

            Console.WriteLine (price.ToString (Formatting.None));
            Console.WriteLine();

            message = string.Format ("{0}GET/v2/accounts", unixTimestamp.ToString ());

            bytes = Encoding.UTF8.GetBytes (message);
            rawHmac = hmac.ComputeHash (bytes);
            signature = rawHmac.ByteArrayToHexString ();

            var jsonCode = host
                .AppendPathSegment ("v2/accounts")
                .WithHeader ("CB-ACCESS-SIGN", signature)
                .WithHeader ("CB-ACCESS-TIMESTAMP", unixTimestamp)
                .WithHeader ("CB-ACCESS-KEY", apiKey)
                .WithHeader ("CB-VERSION", apiVersion)
                .GetJsonAsync<dynamic> ()
                .Result;

            Console.WriteLine (jsonCode.ToString (Formatting.None));

           dynamic stuff = null;
           try {
               stuff = JsonConvert.DeserializeObject(jsonCode.ToString (Formatting.None));
           }
           catch(Exception) {
               Console.Write("Error deserializing");
           }

           int count = stuff.data.Count;

           for(int i = 0; i < count; i++) {
                string currAmount = stuff.data[i].balance.amount;
                string currCode = stuff.data[i].balance.currency;
                Console.WriteLine(currCode + ": " + currAmount);
           }

           Console.ReadLine ();
        }
    }
}