简体   繁体   English

Gdax API对POST请求返回无效签名

[英]Gdax api returning invalid signature on POST requests

Im trying to integrate with GDAX-API and i've successfully managed to place GET calls and receive an answer, however when i'm trying to make a POST call i get the following answer {"message":"invalid signature"} 我正在尝试与GDAX-API集成,并且已经成功地进行了GET调用并收到了应答,但是当我尝试进行POST调用时,我得到以下回答{“ message”:“ invalid signature”}

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

but im not sure if this is the problem... 但我不确定这是否是问题...

My signature part is based on the java library mentioned by Gdax https://github.com/irufus/gdax-java 我的签名部分基于Gdax https://github.com/irufus/gdax-java提到的Java库

here is the interesting part of my code 这是我代码中有趣的部分

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."));

        }
    }

and I've printed the json in the test request 我已经在测试请求中打印了json

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

EDIT!!!!!!! 编辑!!!!!!!

I had an issue with the time stamp for some reason there is no need to drop the last 3 digits (ms) so im passing it as is but now im getting 由于某种原因,我遇到了时间戳问题,无需删除最后3个数字(ms),因此即时传递,但现在即时获取

{"message":"request timestamp expired"} {“消息”:“请求时间戳过期”}

The timestamp has to be seconds since the Unix epoch (Jan 1, 1970). 自Unix时代(1970年1月1日)以来,时间戳必须为​​秒。

You may include the microseconds, but you must have a period (ie 123.456 ). 您可以包括微秒,但必须有一个句点(即123.456 )。

The message error you are getting is because the time is more than a few seconds off between your server and the GDAX server. 您收到的消息错误是因为服务器与GDAX服务器之间的时间相差几秒钟。 It is likely that of you do not include a period for the ms that it is off by about ×1000... 您可能没有为ms加上大约×1000的周期...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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