我正在尝试与GDAX-API集成,并且我成功地进行了GET调用并收到了答案,但是当我尝试进行POST调用时,我得到以下答案
{“消息”:“无效签名”}
我在这里看到了一些东西:https://www.reddit.com/r/GDAX/comments/7twdfv/gdax_api_invalid_signature_problem/
但我不确定这是否是问题...
我的签名部分基于Gdax https://github.com/irufus/gdax-java提到的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位数字(ms)
所以我现在就过去了
{“消息”:“请求时间戳过期”}
最佳答案
自Unix时代(1970年1月1日)以来,时间戳必须为秒。
您可以包括微秒,但必须有一个句点(即123.456
)。
您收到的消息错误是因为服务器与GDAX服务器之间的时间相差几秒钟。您可能没有为ms加上约1000×的周期...