我正试图在GDAX上发布一个帖子请求。但我总是收到一条“无效签名”的信息。用于创建请求+签名的GDAX文档:https://docs.gdax.com/#creating-a-request
Pres散列字符串返回以下内容:
订单{“价格”:“1000.0”,“大小”:“0.02”,“类型”:“限制”,“侧”:“出售”,"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请求调用,但它返回相同的消息(“无效签名”)。
有什么线索吗?
非常感谢您提前!
发布于 2017-12-19 16:43:40
看起来您正在对价格订单数据进行签名,这是一个字符串,但是对于post中的主体,您正在将其转换为一个json节点。当gdax解码签名并将有效负载数据与已解密的数据(签名体)进行比较时,这些数据可能不匹配。
为什么不直接发送字符串作为主体并删除".asJson“呢?
.body(b)在C#中测试API时,我遇到了一个类似的问题。经过三个下午的尝试。我测试了如何将数据作为字符串发送,并且能够传递无效的签名错误。
发布于 2017-09-30 15:19:34
我也有同样的问题。
我用了http:
但正确的一个httpS:
问题解决了。
https://stackoverflow.com/questions/45233140
复制相似问题