使用Java访问Poloniex HTTP API
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Java访问Poloniex HTTP API相关的知识,希望对你有一定的参考价值。
我尝试连接到poloniex.com API https://poloniex.com/support/api/,其中说:
(所有对交易API的调用都通过HTTP POST发送到https://poloniex.com/tradingApi,并且必须包含以下标题:
- 密钥 - 您的API密钥。
- Sign - 根据HMAC-SHA512方法,由密钥的“秘密”签名的查询的POST数据。
此外,所有查询都必须包含“nonce”POST参数。 nonce参数是一个整数,必须始终大于之前使用的nonce。)
但我总是得到
{"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")));
另一答案
我自己也在努力,终于开始工作了。这是一个非常基本的工作示例:
public class PoloTest {
public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException, ClientProtocolException, IOException {
String key = "YOUR API KEY HERE";
String secret = "YOUR API SECRET HERE";
String url = "https://poloniex.com/tradingApi";
String nonce = String.valueOf(System.currentTimeMillis());
String queryArgs = "command=returnBalances&nonce=" + nonce;
Mac shaMac = Mac.getInstance("HmacSHA512");
SecretKeySpec keySpec = new SecretKeySpec(secret.getBytes(), "HmacSHA512");
shaMac.init(keySpec);
final byte[] macData = shaMac.doFinal(queryArgs.getBytes());
String sign = Hex.encodeHexString(macData);
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost(url);
post.addHeader("Key", key);
post.addHeader("Sign", sign);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("command", "returnBalances"));
params.add(new BasicNameValuePair("nonce", nonce));
post.setEntity(new UrlEncodedFormEntity(params));
CloseableHttpResponse response = httpClient.execute(post);
HttpEntity responseEntity = response.getEntity();
System.out.println(response.getStatusLine());
System.out.println(EntityUtils.toString(responseEntity));
}
}
另一答案
nonce参数必须与命令一起MAC'...如果一个哈希是一个单向函数,并且Polo不知道我可能选择哪个nonce,(或者当我使用UTC时),Polo怎么样从我发送的东西中提取任何有意义的东西。
以上是关于使用Java访问Poloniex HTTP API的主要内容,如果未能解决你的问题,请参考以下文章
Poloniex C# Trading API webRequest 回来了 (403) Forbidden