gpt4 book ai didi

java - Gdax api 在 POST 请求上返回无效签名

转载 作者:太空宇宙 更新时间:2023-11-04 10:33:58 25 4
gpt4 key购买 nike

我正在尝试与 GDAX-API 集成,并且我已经成功地发出 GET 调用并收到答案,但是当我尝试进行 POST 调用时,我得到以下答案{“message”:“无效签名”}

我在这里看到了一些东西: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":"request timestamp expired"}

最佳答案

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

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

您收到的消息错误是因为您的服务器和 GDAX 服务器之间的时间相差超过几秒。您可能没有包含毫秒的句点,它相差约 ×1000...

关于java - Gdax api 在 POST 请求上返回无效签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49679288/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com