Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
586 views
in Technique[技术] by (71.8m points)

asp.net - 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

question from:https://stackoverflow.com/questions/65866159/coinbase-api-invalid-signature-c-sharp-net-core-and-httpclient

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...