Recently, as a bit of a personal curiosity project, I have been writing an incredibly simple bot to use on Poloniex (a cryptocurrency exchange). I have been able to get the public API to work properly, but when I started testing the trading API, things started to fall apart. When I run this (and the 30 something other variations in an attempt to get things to work) all I get is
{"error":"Invalid command."}
. I am new to using basically every single one of the libraries I am using, so there's a huge margin for error that I can't narrow down.
Poloniex API documentation is here: https://poloniex.com/support/api/
public String returnBalances() {
try {
long nonce = System.nanoTime();
String params = "command=returnBalances&nonce=" + nonce;
URL u = new URL(URL_PRIVATE + "?" + params);
String sign = getSign(params);
if(sign == null) return null;
HttpURLConnection huc = (HttpURLConnection) u.openConnection();
huc.setRequestMethod("GET");
huc.setRequestProperty("Key", API_Key);
huc.setRequestProperty("Sign", sign);
huc.setRequestProperty("nonce", String.valueOf(nonce));
return getDataFromHUC(huc);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private String getSign(String c) {
try {
SecretKeySpec mac_key = new SecretKeySpec(API_secret.getBytes(), "HmacSHA512");
Mac mac = Mac.getInstance("HmacSHA512");
mac.init(mac_key);
String sign = bytesToHex(mac.doFinal((c).getBytes()));
return sign;
}
catch (Exception e) {
return null;
}
}
private String getDataFromHUC(HttpURLConnection huc) {
try {
huc.connect();
BufferedReader br = new BufferedReader(new InputStreamReader(huc.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line+"\n");
}
br.close();
String data = sb.toString();
return data;
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}
Furthermore, while I was googling stuff to find an answer, I found that not a single person uses the java.net
libraries (which I am). Have I completely missed base on what I should be doing? I would prefer to use these libraries if possible so I can really learn what's going on behind the scenes as much as I am able.
Sorry if there's an obvious answer either here or somewhere else on this website. I've been at this for hours now with no progress so I'm bound to make mistakes.
P.S. I am aware that my run-time error handling is probably pretty low quality, but I haven't had any problems with it so improving it is kind of a low priority.
As described in the provided documentation, the whole TradingAPI requires POST requests.
So instead of huc.setRequestMethod("GET");
you'll need huc.setRequestMethod("POST");
You'll also probably need to enable in- and outputs after that:
huc.setDoInput(true);
huc.setDoOutput(true);
And don't forget to set your Content-Type and Content-Length:
huc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
huc.setRequestProperty("Content-Length", params.length());
And instead of adding the parameters to the end of your URL, you need to write them to the output (right after setting the request properties):
DataOutputStream os = new DataOutputStream(huc.getOutputStream());
os.writeBytes(params);
os.flush();
It also seams like you don't need to send the nonce as a request property.
I hope this helps!