【问题标题】:Coinbase API Invalid SIgnature. C# .net core and httpClientCoinbase API 签名无效。 C# .net 核心和 httpClient
【发布时间】:2021-01-24 00:57:09
【问题描述】:

所以我正在尝试完成获取我的帐户的相当基本的任务。每当我尝试这样做时,我都会收到无效签名的响应。对我来说似乎很好,我确保访问符号都是小写的并且它是十六进制编码的。不知道还有什么可能。 请求与我的数据看起来像这样:

{Method: GET, RequestUri: 'https://api.coinbase.com/v2/accounts', Version: 1.1, Content: <null>, Headers:
{
  CB-ACCESS-KEY: g4zV4eOfL7d2f6ok
  CB-ACCESS-SIGN: 892fc726520[other characters in here]6eb31430d3e0a7511e408f
  CB-ACCESS-TIMESTAMP: 1611449629
}}
public void DisplayAccountData()
        {
            HttpClient httpClient = new HttpClient();
            List<CoinData> coinsData = new List<CoinData>();
            string resultsString = null;
            string coinDataURL = string.Format("https://api.coinbase.com/v2/accounts");
            DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
            TimeSpan diff = DateTime.Now.ToUniversalTime() - origin;
            var timestamp = (long)Math.Floor(diff.TotalSeconds);
            string message = timestamp.ToString() + "GET" + "https://api.coinbase.com/v2/accounts";            
            var signature = GenerateSignature(timestamp.ToString(), "GET", coinDataURL, "", secretKey);
            
            var request = new HttpRequestMessage
            {
                RequestUri = new Uri(coinDataURL),
                Method = HttpMethod.Get,
                Headers =
                {
                    { "CB-ACCESS-KEY", apiKey },
                    { "CB-ACCESS-SIGN", signature },
                    { "CB-ACCESS-TIMESTAMP", timestamp.ToString() },
                },
            };
            HttpResponseMessage response = httpClient.SendAsync(request).Result;
            HttpContent responseContent = response.Content;
            using (var reader = new StreamReader(responseContent.ReadAsStreamAsync().Result))
            {
                // Write the output.
                resultsString = reader.ReadToEndAsync().Result;
            }
            if (resultsString != null)
            {
                //get the json result and go down one level and update the result
                JObject coinDataJSON = JObject.Parse(resultsString);
                string jsonDataObject = coinDataJSON.GetValue("data").ToString();
                coinDataJSON = JObject.Parse(jsonDataObject);
                CoinData coinData = new CoinData
                {
                    Symbol = coinDataJSON.GetValue("base").ToString(),
                    Amount = coinDataJSON.GetValue("amount").ToString() + " " + coinDataJSON.GetValue("currency").ToString()
                };
                coinsData.Add(coinData);
            }
            Console.WriteLine();
        }
        public static string GenerateSignature(string timestamp, string method, string url, string body, string appSecret)
        {
            return GetHMACInHex(appSecret, timestamp + method + url + body);
        }
        public static string GetHMACInHex(string key, string data)
        {
            var hmacKey = Encoding.UTF8.GetBytes(key);
            var dataBytes = Encoding.UTF8.GetBytes(data);

            using (var hmac = new HMACSHA256(hmacKey))
            {
                var sig = hmac.ComputeHash(dataBytes);
                return ByteToHexString(sig);
            }
        }
        static string ByteToHexString(byte[] bytes)
        {
            char[] c = new char[bytes.Length * 2];
            int b;
            for (int i = 0; i < bytes.Length; i++)
            {
                b = bytes[i] >> 4;
                c[i * 2] = (char)(87 + b + (((b - 10) >> 31) & -39));
                b = bytes[i] & 0xF;
                c[i * 2 + 1] = (char)(87 + b + (((b - 10) >> 31) & -39));
            }
            return new string(c);
        }

任何帮助将不胜感激。 这里也是api文档的链接https://developers.coinbase.com/docs/wallet/api-key-authentication

【问题讨论】:

标签: c# asp.net asp.net-core httpclient coinbase-api


【解决方案1】:

您正在使用完整的 http url 生成消息和签名,应该只是 url 的路径 即“https://api.coinbase.com/v2/accounts” 应该只是 /v2/帐户

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-09
    • 1970-01-01
    • 1970-01-01
    • 2019-01-12
    • 2015-04-21
    • 1970-01-01
    • 2015-10-17
    • 2019-04-07
    相关资源
    最近更新 更多