我尝试连接到poloniex.com API https://poloniex.com/support/api/,它说:
(对交易API的所有调用都通过HTTP发送到https://poloniex.com/tradingApi,并且必须包含以下标头:
此外,所有查询都必须包含"nonce“POST参数。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();
}
}
}发布于 2016-09-07 19:39:09
我查看了他们在页面上链接到的Python示例。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")));发布于 2016-09-29 14:29:37
我自己也和这件事做了斗争,终于开始工作了。下面是一个非常基本的工作示例:
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));
}
}发布于 2017-02-08 14:52:28
现在的参数必须和命令一起修改.如果散列是一个单向函数,而Polo不知道我现在可以选择什么(或者,如果我使用UTC),那么Polo如何从我发送的内容中提取任何有意义的内容。
https://stackoverflow.com/questions/39369649
复制相似问题