I'm quite new in coding and was hoping I get an advice how to solve my problem. I'm writing a java code to connect to trade.io API, but I can't figure out how to submit the correct encrypted message to the exchange using "POST" and "DELETE".
So far, I managed to work out how to receive information with "GET", but no success on the others.
Here is what I wrote so far:
/*
* CancelOrder cancels an existing order ==> This doesn't work!
*/
public String CancelOrder(String orderId) throws MalformedURLException,
IOException {
return signAndSend("/order/" + orderId, "DELETE");
}
/*
* Reads the open orders in the account ==> This works!
*/
public String getOpenOrders(String symbol) throws MalformedURLException,
IOException {
return signAndSend("/openOrders/" + symbol, "GET");
}
/*
* Signs and Sends signature. This method is called when signature is
needed.
*/
private String signAndSend(String url, String method) throws
MalformedURLException, IOException {
String nonce = String.valueOf(System.currentTimeMillis());
String baseUrl = UrlTradeio.urlV1;
String ts = "?ts=" + nonce;
String sign = hmac512Digest(ts, TRADEIO_SECRET_KEY).toUpperCase();
HttpURLConnection con = (HttpURLConnection) new URL(baseUrl + url + ts).openConnection();
con.setRequestProperty("Sign", sign);
con.setRequestProperty("Key", TRADEIO_API_KEY);
con.setRequestMethod(method);
con.connect();
InputStream response = con.getInputStream();
//
try (Scanner scanner = new Scanner(response)) {
String responseBody = scanner.next();
return responseBody;
}
}
The exchange provided very thorough example in C# here:
https://github.com/tradeio/api-csharpclient/blob/master/Tradeio.Client/TradeioApi.cs
Here is the output for "getOpenOrders" and the error message when I try to close the same.
ktos_eth open orders are: {"code":0,"timestamp":1559338453064,"orders":[{"orderId":"-72057593948251267","total":"0.0","orderType":"limit","commission":"0.0","createdAt":"2019-01-23T17:36:55.8633777Z","unitsFilled":"0.0","isPending":true,"status":"Working","type":"sell","requestedAmount":"75.0","baseAmount":"0.0","quoteAmount":"0.0","price":"0.00014000","isLimit":true,"loanRate":"0.0","rateStop":"0.0","instrument":"ktos_eth","requestedPrice":"0.00014000","remainingAmount":"75.0"}]}
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: https://api.exchange.trade.io/api/v1/order/-72057593948251267?ts=1559338452695 at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1913)
Thanks in advance for looking into it
Cheers Alex