binance-futures-connector-java
binance-futures-connector-java copied to clipboard
batchOrders -> Signature for this request is not valid
`final static String HMAC_SHA256 = "HmacSHA256";
//convert byte array to hex string
private static String bytesToHex(byte[] bytes) {
final char[] hexArray = "0123456789abcdef".toCharArray();
char[] hexChars = new char[bytes.length * 2];
for (int j = 0, v; j < bytes.length; j++) {
v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}`
` public static String getSignature(String data, String key) {
byte[] hmacSha256 = null;
try {
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), HMAC_SHA256);
Mac mac = Mac.getInstance(HMAC_SHA256);
mac.init(secretKeySpec);
hmacSha256 = mac.doFinal(data.getBytes());
} catch (Exception e) {
throw new RuntimeException("Failed to calculate hmac-sha256", e);
}
return bytesToHex(hmacSha256);
}`
`queryString = "batchOrders=[{"symbol":"BTCUSDT","side":"SELL","type":"LIMIT","price":"37300","timeInForce":"GTC","quantity":1.0},{"symbol":"BTCUSDT","side":"BUY","type":"TAKE_PROFIT_MARKET","stopPrice":"37000","timeInForce":"GTC","quantity":1.0},{"symbol":"BTCUSDT","side":"BUY","type":"STOP_MARKET","stopPrice":"37900","timeInForce":"GTC","quantity":1.0}]×tamp=1700531873609"`
`String signature = Signature.getSignature(queryString, secretKey);`
https://testnet.binancefuture.com/fapi/v1/batchOrders?batchOrders=[{"symbol":"BTCUSDT","side":"SELL","type":"LIMIT","price":"37300","timeInForce":"GTC","quantity":1.0},{"symbol":"BTCUSDT","side":"BUY","type":"TAKE_PROFIT_MARKET","stopPrice":"37000","timeInForce":"GTC","quantity":1.0},{"symbol":"BTCUSDT","side":"BUY","type":"STOP_MARKET","stopPrice":"37900","timeInForce":"GTC","quantity":1.0}]×tamp=1700531873609&signature=9e5d7f6a32aa1fa48e909d8849394e2f1a5cbd11075d259fef1daf6f33cabe94
When I call the placeMultipleOrders api the result returns the error {"code":-1022,"msg":"Signature for this request is not valid."}. How can I fix it?