Accessing poloniex HTTP API using java

I tried to connect to poloniex com API https://poloniex.com/support/api/ , which says:

But I always get

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

My hmac512 digest works normally. I have checked it

There must be something wrong with my code

Can I help you?

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

Solution

I looked at the python example they linked on the page The nonce parameter must be used with the command Mac, and the final MAC is appended in hexadecimal encoding format:

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

In addition, 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")));
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>