api - GDAX Post Call 返回无效签名

标签 api post request coinbase-api

我正在尝试在 GDAX 上发出帖子请求。
但我总是收到“无效签名”消息。
用于创建请求 + 签名的 GDAX API 文档:https://docs.gdax.com/#creating-a-request

Preshash 字符串返回以下内容:

1500627733POST/orders{"price":"1000.0","size":"0.02","type":"limit","side":"sell","product_id":"BTC-EUR"}



我的签名方法:
public String generateSignature(String requestPath, String method, String body, String timestamp) {
        try {
            String prehash = timestamp + method.toUpperCase() + requestPath + body;
            byte[] secretDecoded = Base64.getDecoder().decode(secretKey);
            SecretKeySpec keyspec = new SecretKeySpec(secretDecoded, "HmacSHA256");
            Mac sha256 = (Mac) Mac.getInstance("HmacSHA256").clone();
            sha256.init(keyspec);
            return Base64.getEncoder().encodeToString(sha256.doFinal(prehash.getBytes()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

我的请求方法:
private boolean placeLimitOrder(String currencyPair, String side, String price, String size)
            throws UnirestException {

        String timestamp = Instant.now().getEpochSecond() + "";
        String api_method = "/orders";
        String path = base_url + api_method; //base_url = https://api.gdax.com
        String method = "POST";
        String b = "{\"price\":\"1000.0\",\"size\":\"0.02\",\"type\":\"limit\",\"side\":\"sell\",\"product_id\":\"BTC-EUR\"}";
        JsonNode n = new JsonNode(b);
        String sig = generateSignature(api_method, method,b, timestamp);

        HttpResponse<JsonNode> rep = Unirest.post(path).header("accept", "application/json")
                .header("content-type", "application/json")
                .header("CB-ACCESS-KEY", publicKey)
                .header("CB-ACCESS-PASSPHRASE", passphrase)
                .header("CB-ACCESS-SIGN", sig)
                .header("CB-ACCESS-TIMESTAMP", timestamp)
                .body(n)
                .asJson();

        System.out.println(rep.getStatusText()); //Bad Request

        System.out.println(rep.getBody().toString()); //invalid signature

        System.out.println(sig); //returns something


        return false;
    }

我还尝试使用 Insomnia 进行 API 请求调用,但它返回相同的消息(“无效签名”)。

有什么线索吗?

非常感谢您提前!

最佳答案

看起来您正在签署价格订单数据,这是一个字符串,但对于帖子中的正文,您将其转换为 json 节点。当 gdax 解码签名并将有效负载数据与收到的解密(签名正文)进行比较时,这可能不匹配。

为什么不将字符串作为正文发送并删除“.asJson”?

.body(b)

我在 C# 中测试 API 时遇到了类似的问题。经过3个下午的尝试。我测试了将数据作为字符串发送,并且能够通过无效签名错误。

关于api - GDAX Post Call 返回无效签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45233140/

相关文章:

php - PHP 中的 Bing 搜索 API(获取下一个结果)无法正常工作

api - 关闭窗口,登录后重定向到旧页面[客户端程序]

java - 最简单的Java图像处理方案

java - HTTP POST查询如何计算内容长度

java - getInputStream() 在 POST 请求上挂起

java - 在/share 和/alfresco 之间的 POST 请求上的 Alfresco 随机 HTTP 400

快速 cookies urlsession 请求

request - 如何使用 axios 发送基本身份验证

php - Laravel 5.2 Codeception 功能测试问题与 PUT/PATCH 请求

ios - NSMutableURLRequest 和 JSON - 我将如何完成此连接?