如何从Coinbase读取帐户余额

时间:2018-01-13 07:56:58

标签: c# api coinbase-api

我能够运行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 ();
       }
    }
}

2 个答案:

答案 0 :(得分:1)

没有总帐户值返回。

您正在访问/ accounts端点,该端点应返回格式化为JSON对象的分页列表。你必须解析每个元素以找到你正在寻找的东西,做一些数学运算,并创建一个总计。

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

答案 1 :(得分:0)

此代码工作正常。我将API升级到最新版本(日期是2018-01-13)。我还删除了所有API密钥,并为多种货币创建了一个。我只能在开始时看到API Secret,当我创建新密钥时 - 以后它不再显示了。我偶然注意到,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 ();
        }
    }
}