1

我正在尝试与 GDAX-API 集成,并且我已经成功地进行了 GET 调用并收到了答案,但是当我尝试进行 POST 调用时,我得到了以下答案 {"message":"invalid signature"}

我在这里看到了一些东西:https ://www.reddit.com/r/GDAX/comments/7twdfv/gdax_api_invalid_signature_problem/

但我不确定这是否是问题所在......

我的签名部分是基于Gdax提到的java库https://github.com/irufus/gdax-java

这是我的代码中有趣的部分

public String purchaseOrder(String jsonOrder) throws ClientProtocolException, IOException {
        CloseableHttpClient client = HttpClientBuilder.create().build();
        HttpPost request = new HttpPost(BASE_URL + "/orders");
        String timestamp = Instant.now().getEpochSecond() + "";
        request.addHeader("accept", "application/json");
        request.addHeader("content-type", "application/json");
        request.addHeader("User-Agent", "gdax-java-client");
        request.addHeader(CB_ACCESS_KEY, API_KEY);
        request.addHeader(CB_ACCESS_SIGN, generateSignedHeader("/orders", "POST", jsonOrder, String.valueOf(timestamp)));
        request.addHeader(CB_ACCESS_TIMESTAMP, String.valueOf(timestamp));
        request.addHeader(CB_ACCESS_PASSPHRASE, PASSPHRASE);

        HttpResponse response = client.execute(request);
        String jsonResponse = EntityUtils.toString(response.getEntity(), "UTF-8");
        client.close();
        return jsonResponse;
    }

    private String generateSignedHeader(String requestPath, String method, String body, String timestamp) {
        try {
            String prehash = timestamp + method.toUpperCase() + requestPath + body;
            byte[] secretDecoded = Base64.getDecoder().decode(API_SECRET);
            SecretKeySpec keyspec = new SecretKeySpec(secretDecoded, Mac.getInstance("HmacSHA256").getAlgorithm());
            Mac sha256 = (Mac) Mac.getInstance("HmacSHA256").clone();
            sha256.init(keyspec);
            String response = Base64.getEncoder().encodeToString(sha256.doFinal(prehash.getBytes()));
            System.out.println(response);
            return response;
        } catch (CloneNotSupportedException | InvalidKeyException e) {
            System.out.println(e);
            throw new RuntimeErrorException(new Error("Cannot set up authentication headers."));
        } catch (NoSuchAlgorithmException e) {
            System.out.println(e);
            throw new RuntimeErrorException(new Error("Cannot set up authentication headers."));

        }
    }

我已经在测试请求中打印了 json

{"side":"buy","type":"market","product_id":"BTC-USD","size":0.01000000000000000020816681711721685132943093776702880859375}

编辑!!!!!!!

由于某种原因,我的时间戳有问题,不需要删除最后 3 位数字(毫秒),所以我按原样传递它,但现在我得到了

{"message":"请求时间戳过期"}

4

1 回答 1

0

时间戳必须是自 Unix 纪元(1970 年 1 月 1 日)以来的秒数。

您可以包括微秒,但您必须有一个句点(即123.456)。

您收到的消息错误是因为您的服务器和 GDAX 服务器之间的时间超过了几秒钟。你们中可能不包括一个毫秒的时间段,它关闭了大约×1000 ...

于 2018-04-10T18:05:08.367 回答