public static String POLONIEX_SECRET_KEY = "secret"; //KEY
public static String POLONIEX_API_KEY = "api"; // TODO API KEY
public static void main(String[] args) {
try {
accessPoloniex();
} catch (IOException e) {
e.printStackTrace();
}
}
public static final long generateNonce(){
Date d = new Date();
return d.getTime();
}
public static final void accessPoloniex() throws IOException{
String nonce = new BigDecimal(Poloniex.generateNonce()).toString();
String connectionString = "https://poloniex.com/tradingApi";
String queryArgs = "command=returnDepositAddresses&nonce="+nonce;
String hmac512 = hmac512Digest(queryArgs, POLONIEX_SECRET_KEY);
// Produce the output
ByteArrayOutputStream out = new ByteArrayOutputStream();
Writer writer = new OutputStreamWriter(out, "UTF-8");
writer.append(queryArgs);
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost(connectionString);
post.setHeader("Key", POLONIEX_API_KEY);
post.setHeader("Sign", hmac512);
// post.setHeader("Content-Type", "application/json");
post.setEntity(new ByteArrayEntity(queryArgs.getBytes()));
List
params.add(new BasicNameValuePair("nonce", nonce));
CloseableHttpResponse response = null;
try
{
// post.setEntity(new UrlEncodedFormEntity(params));
response = httpClient.execute(post);
System.out.println(response.getStatusLine());
String result = EntityUtils.toString(response.getEntity());
System.out.println("result: "+result);
} finally
{
response.close();
}
}
public static String hmac512Digest(String msg, String keyString) {
Mac shaMac;
try {
shaMac = Mac.getInstance("HmacSHA512");
SecretKeySpec keySpec = new SecretKeySpec(keyString.getBytes(), "HmacSHA512");
shaMac.init(keySpec);
final byte[] macData = shaMac.doFinal(msg.getBytes());
// return Hex.encodeHexString(macData); //again with try/catch for InvalidKeyException
return Base64.getEncoder().encodeToString(macData);
} catch (Exception e1) {
e1.printStackTrace();
}
return null;
}
}