Accessing Poloniex HTTP API with Java -
i try connect poloniex.com api https://poloniex.com/support/api/ says:
(all calls trading api sent via http post https://poloniex.com/tradingapi , must contain following headers:
- key - api key.
- sign - query's post data signed key's "secret" according hmac-sha512 method.
additionally, queries must include "nonce" post parameter. nonce parameter integer must greater previous nonce used.)
but
{"error":"invalid api key\/secret pair."}
my hmac512digest
works fine, i've checked it.
there must wrong in code.
can 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 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); } { in.close(); response.close(); } } }
i've looked python example they've linked on page. nonce parameter must mac'ed along command , final mac appended in hex-encoded format:
string queryargs = "command=returnbalances&nonce=" + nonce; string hmac512 = hmac512digest(queryargs, poloniex_secret_key);
also, following
bytearrayoutputstream out = new bytearrayoutputstream(); writer writer = new outputstreamwriter(out, "utf-8"); writer.append(queryargs); writer.flush(); //... post.setentity(new bytearrayentity(out.tobytearray()));
can reduced
post.setentity(new bytearrayentity(queryargs.getbytes("utf-8")));
Comments
Post a Comment