首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Java访问Poloniex HTTP

用Java访问Poloniex HTTP
EN

Stack Overflow用户
提问于 2016-09-07 12:12:05
回答 3查看 3.2K关注 0票数 3

我尝试连接到poloniex.com API https://poloniex.com/support/api/,它说:

(对交易API的所有调用都通过HTTP发送到https://poloniex.com/tradingApi,并且必须包含以下标头:

  • 密钥-你的API密钥。
  • 签名-根据HMAC- The 512方法,查询的POST数据由您的密钥的“机密”签名。

此外,所有查询都必须包含"nonce“POST参数。nonce参数是一个整数,必须始终大于以前使用的整数。)

但我总是

代码语言:javascript
复制
{"error":"Invalid
API key\/secret pair."}

我的hmac512Digest很好,我已经检查过了。

我的代码一定有问题。

有人能帮忙吗?

代码语言:javascript
复制
   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();
        }
    }
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-09-07 19:39:09

我查看了他们在页面上链接到的Python示例。nonce参数必须与命令一起添加MAC‘,最后的MAC以十六进制编码格式追加:

代码语言:javascript
复制
String queryArgs = "command=returnBalances&nonce=" + nonce;
String hmac512 = hmac512Digest(queryArgs, POLONIEX_SECRET_KEY);

另外,以下几点

代码语言:javascript
复制
ByteArrayOutputStream out = new ByteArrayOutputStream();
Writer writer = new OutputStreamWriter(out, "UTF-8");
writer.append(queryArgs);
writer.flush();
//...
post.setEntity(new ByteArrayEntity(out.toByteArray()));

可以被还原为

代码语言:javascript
复制
post.setEntity(new ByteArrayEntity(queryArgs.getBytes("UTF-8")));
票数 2
EN

Stack Overflow用户

发布于 2016-09-29 14:29:37

我自己也和这件事做了斗争,终于开始工作了。下面是一个非常基本的工作示例:

代码语言:javascript
复制
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));
  }

}
票数 4
EN

Stack Overflow用户

发布于 2017-02-08 14:52:28

现在的参数必须和命令一起修改.如果散列是一个单向函数,而Polo不知道我现在可以选择什么(或者,如果我使用UTC),那么Polo如何从我发送的内容中提取任何有意义的内容。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39369649

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档