N-rG N-rG - 2 months ago 31
HTTP Question

Accessing Poloniex HTTP API with Java

I try to connect to the poloniex.com API https://poloniex.com/support/api/ which says:


(All calls to the trading API are sent via HTTP POST to https://poloniex.com/tradingApi and must contain the following headers:


  • Key - Your API key.

  • Sign - The query's POST data signed by your key's "secret" according to the HMAC-SHA512 method.



Additionally, all queries must include a "nonce" POST parameter. The nonce parameter is an integer which must always be greater than the previous nonce used.)


But I always get

{"error":"Invalid
API key\/secret pair."}


My
hmac512Digest
works fine, I've checked it.

There must be something wrong in my code.

Can someone please Help?

public class Pol2 {

public static String POLONIEX_SECRET_KEY = "12345";
public static String POLONIEX_API_KEY = "ABX";


public static void main(String[] args) {
try {
accessPoloniex();
} catch (IOException e) {
e.printStackTrace();
}
}


public static final void accessPoloniex() throws IOException {

final String nonce = String.valueOf(System.currentTimeMillis());

String connectionString = "https://poloniex.com/tradingApi";

String queryArgs = "command=returnBalances";

String hmac512 = hmac512Digest(queryArgs, POLONIEX_SECRET_KEY);

// Produce the output
ByteArrayOutputStream out = new ByteArrayOutputStream();
Writer writer = new OutputStreamWriter(out, "UTF-8");
writer.append(queryArgs);
writer.flush();

CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost(connectionString);
post.addHeader("Key", POLONIEX_API_KEY); //or setHeader?
post.addHeader("Sign", hmac512); //or setHeader?

post.setEntity(new ByteArrayEntity(out.toByteArray()));
List<NameValuePair> params = new ArrayList<>();

params.add(new BasicNameValuePair("command", "returnBalances"));
params.add(new BasicNameValuePair("nonce", nonce));

CloseableHttpResponse response = null;
Scanner in = null;
try {
post.setEntity(new UrlEncodedFormEntity(params));
response = httpClient.execute(post);
HttpEntity entity = response.getEntity();
in = new Scanner(entity.getContent());
while (in.hasNext()) {
System.out.println(in.next());
}
EntityUtils.consume(entity);
} finally {
in.close();
response.close();
}
}
}

Answer

I've looked into the Python example that they've linked to on their page. The nonce parameter must be MAC'ed along with the command and the final MAC is appended in Hex-encoded format:

String queryArgs = "command=returnBalances&nonce=" + nonce;
String hmac512 = hmac512Digest(queryArgs, POLONIEX_SECRET_KEY);

Also, the following

ByteArrayOutputStream out = new ByteArrayOutputStream();
Writer writer = new OutputStreamWriter(out, "UTF-8");
writer.append(queryArgs);
writer.flush();
//...
post.setEntity(new ByteArrayEntity(out.toByteArray()));

can be reduced to

post.setEntity(new ByteArrayEntity(queryArgs.getBytes("UTF-8")));
Comments