使用Java访问Poloniex HTTP API
发布时间:2020-12-15 04:35:53 所属栏目:Java 来源:网络整理
导读:我尝试连接到poloniex.com API https://poloniex.com/support/api/,其中说: (All calls to the trading API are sent via HTTP POST to 07001 and must contain the following headers: Key – Your API key. Sign – The query’s POST data signed by you
我尝试连接到poloniex.com API
https://poloniex.com/support/api/,其中说:
但我总是得到 {"error":"Invalid API key/secret pair."} 我的hmac512Digest运行正常,我已经检查过了. 我的代码肯定有问题. 有人可以帮忙吗? 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(); } } } 解决方法
我查看了他们在页面上链接的
Python example. nonce参数必须与命令一起MAC,并且最终MAC以十六进制编码格式附加:
String queryArgs = "command=returnBalances&nonce=" + nonce; String hmac512 = hmac512Digest(queryArgs,POLONIEX_SECRET_KEY); 另外,以下 ByteArrayOutputStream out = new ByteArrayOutputStream(); Writer writer = new OutputStreamWriter(out,"UTF-8"); writer.append(queryArgs); writer.flush(); //... post.setEntity(new ByteArrayEntity(out.toByteArray())); 可以减少到 post.setEntity(new ByteArrayEntity(queryArgs.getBytes("UTF-8"))); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |