Author

Topic: Access Poloniex trading API from JAVA (Read 1016 times)

full member
Activity: 372
Merit: 130
August 25, 2016, 04:17:34 PM
#4
So I added the nonce to the queryArgs, but still the same result Sad


Code:
    public class Poloniex {

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 = new ArrayList<>();
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;
}
}
full member
Activity: 372
Merit: 130
August 25, 2016, 04:06:08 AM
#3
i am not so familiar with Java so i can't really say anything about the rest of your code but the way you are making nonce (d.getTime()Wink is wrong. google it and you find solutions for generating nonce correctly.

The Poloniex API documentation says: 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.

So I think an increasing number should be correct.


also your queryArgs needs nonce inside of it you generate it but never use it. add &nonce=### to your queryArgs

As I understood it, it should be just added as POST parameter as I did. But I will try to add it to the queryArgs as well.
legendary
Activity: 1946
Merit: 1137
August 25, 2016, 12:44:29 AM
#2
i am not so familiar with Java so i can't really say anything about the rest of your code but the way you are making nonce (d.getTime()Wink is wrong. google it and you find solutions for generating nonce correctly.

also your queryArgs needs nonce inside of it you generate it but never use it. add &nonce=### to your queryArgs
full member
Activity: 372
Merit: 130
August 24, 2016, 04:51:43 PM
#1

I am trying to connect to the Poloniex API with Java, followed the Poloniex documentation and came up with the following code, but for some reason, I always get the error: Invalid command

Any idea what I am doing wrong?
Code:

public static String POLONIEX_SECRET_KEY = "my secret"; //KEY
public static String POLONIEX_API_KEY = "my key"; // TODO API KEY


public static void main(String[] args) {

    try {
        accessPoloniex();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        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(Polo2.generateNonce()).toString();

    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);


    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpPost post = new HttpPost(connectionString);

    post.setHeader("Key", POLONIEX_API_KEY);
    post.setHeader("Sign", hmac512);

    post.setEntity(new ByteArrayEntity(out.toByteArray()));
    List params = new ArrayList<>();
    params.add(new BasicNameValuePair("nonce", nonce));

    CloseableHttpResponse response = null;
    Scanner in = null;
    try
    {
        post.setEntity(new UrlEncodedFormEntity(params));
        response = httpClient.execute(post);
        // System.out.println(response.getStatusLine());
        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();
    }

}

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

    } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    return null;
}
Jump to: