Coinbase API为POST

时间:2018-01-05 01:24:51

标签: c# api coinbase-api

我正在尝试使用Coinbase的API销售加密货币,并且我一直收到401错误。下面的代码适用于我迄今为止尝试过的所有GET方法,但我无法弄清楚POST出错的地方。

    private static string GetWebResponse(string url, string command, string path, string body)
    {
        var timeStamp = EpochSeconds;
        var request = (HttpWebRequest)WebRequest.Create(url);

        request.Method = command;
        request.ContentType = "application/json";
        request.Headers.Add("CB-VERSION", VersionDate);
        request.Headers.Add("CB-ACCESS-KEY", ApiKey);
        request.Headers.Add("CB-ACCESS-SIGN", GetAccessSign(timeStamp, command, path, body));
        request.Headers.Add("CB-ACCESS-TIMESTAMP", timeStamp);

        if (command == "POST")
        {
            using (var streamWriter = new StreamWriter(request.GetRequestStream()))
            {
                streamWriter.Write(body);
                streamWriter.Flush();
                streamWriter.Close();
            }
        }

        var response = (HttpWebResponse)request.GetResponse();

        using (var reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII))
        {
            return reader.ReadToEnd();
        }
    }

    private static string GetAccessSign(string timestamp, string command, string path, string body)
    {
        var hmacKey = Encoding.UTF8.GetBytes(ApiSecret);

        string data = timestamp + command + path + body;
        using (var signatureStream = new MemoryStream(Encoding.UTF8.GetBytes(data)))
        {
            return new HMACSHA256(hmacKey).ComputeHash(signatureStream).Aggregate(new StringBuilder(), (sb, b) => sb.AppendFormat("{0:x2}", b), sb => sb.ToString());
        }
    }

我认为POST请求的正文有问题,但它对我来说是正确的。

编辑:

我将展示身体是如何形成澄清的......

            var body = JsonConvert.SerializeObject(
            new
            {
                commit = "false",
                amount = Math.Round(sellAmount, 8).ToString(),
                currency = "BTC",
                payment_method = fiatWalletId
            });

编辑2:

我尝试了和以前一样的东西,但是使用RestSharp,它会返回一个更具体的错误:

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

编辑3:

此API密钥已设置为所有权限/范围,包括此请求所需的权限:

wallet:accounts:create wallet:accounts:delete wallet:accounts:read wallet:accounts:update wallet:addresses:create wallet:addresses:read wallet:buys:create wallet:buys:read wallet:checkouts:create wallet:checkouts:read wallet:contacts:read wallet:deposits:create wallet:deposits:read wallet:notifications:read wallet:orders:create wallet:orders:read wallet:orders:refund wallet:payment-methods:delete wallet:payment-methods:limits wallet:payment-methods:read wallet:sells:create wallet:sells:read wallet:transactions:read wallet:transactions:request wallet:transactions:send wallet:transactions:transfer wallet:user:email wallet:user:read wallet:user:update wallet:withdrawals:create wallet:withdrawals:read

1 个答案:

答案 0 :(得分:0)

我的解决方案是我忘记将/v2(/v2 +一些路径)放在签名的路径部分并且签名很糟糕,尽管说未经授权,但响应却具有误导性。