简体   繁体   中英

Coinbase API Invalid SIgnature. C# .net core and httpClient

So i am trying to do the fairly basic task of getting my accounts. Whenever i try to do it i get a response of invalid signature. Iit seems fine to me, i have made sure that the access sign is all lower case and that it is hex encoded. not sure what else it could be. The request looks like this with my data:

{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);
        }

Any help would be greatly appreciated. Also here is the link to the api documentation https://developers.coinbase.com/docs/wallet/api-key-authentication

You're generating the message and signature with the full http url, should only be the path of the url ie "https://api.coinbase.com/v2/accounts" should only be /v2/accounts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM