我正在尝试在 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/