Bitfinex API错误消息"密钥符号不存在。"

时间:2018-03-28 06:48:31

标签: java bitcoin

我正在使用Bitfinex API,API版本为1。 但我有一个无法解决的问题。 当我使用' / v1 / order / new'时,服务器发送消息"密钥符号不存在。" 我找不到问题所在。 参数设置如下。 请指教。

========== ========== ========== ==========

/**
    Create Header, Param
*/  
JSONObject json = new JSONObject();
json.put("request", targetURL);
json.put("nonce", Long.toString(getNonce()));
String payload = json.toString();
String payload_base64 = Base64.getEncoder().encodeToString(payload.getBytes());
String payload_sha384hmac = hmacDigest(payload_base64, apiKeySecret, ALGORITHM_HMACSHA384);

HttpTask http = new HttpTask(URL, Method.POST); 
http.addHeader("X-BFX-APIKEY", apiKey);
http.addHeader("X-BFX-PAYLOAD", payload_base64);
http.addHeader("X-BFX-SIGNATURE", payload_sha384hmac);

http.setContentType("x-www-urlencoded");
http.setAcceptType("application/xml");

http.addParam("symbol", "btcusd");
http.addParam("amount", "0.01");
http.addParam("price", "0.01");
http.addParam("side", "buy");
http.addParam("type", "exchange market");
http.addParam("is_hidden", "false");
http.addParam("is_postonly", "true");
http.addParam("use_all_available", "0");
http.addParam("exchange", "bitfinex");
http.addParam("ocoorder", "false");
http.addParam("buy_price_oco", "0");


/**
    Parsing Param
*/  
StringBuilder sb = new StringBuilder();
Set<String> key = m_params.keySet();
int totalCount = key.size();
if (totalCount > 0) {
    int index = 0;
    for (Iterator<String> iterator = key.iterator(); iterator.hasNext();) {
        String keyValue = (String) iterator.next();
        String valueValue = (String) m_params.get(keyValue);
        sb.append(String.format("%s=%s", keyValue, valueValue));
        if (index < totalCount - 1) {
            sb.append("&");
        }
        index++;
    }

    query = sb.toString();
}

/**
    send Param
*/
if (!query.isEmpty()) {
    DataOutputStream wr;
    try {
        wr = new DataOutputStream(m_connection.getOutputStream());
        wr.writeBytes(query);
        wr.flush();
        wr.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:0)

您需要将所有参数放入有效负载对象中。

这是我在JAVASCRIPT上的例子:

auth_v1_request(path, params){

return new Promise((resolve, reject) => {

  // console.log(this.account);

  const apiKey = this.account.api_key;
  const apiSecret = this.account.api_secret;

  const apiPath = '/' + path;

  const nonce = (Date.now() * 1000).toString();

  const completeURL = `${ CONFIG.BITFINEX.API_URL }${apiPath}`;

  params.nonce = nonce;
  params.request = apiPath;

  const payload = new Buffer(JSON.stringify(params))
    .toString('base64');

  const signature = crypto
    .createHmac('sha384', apiSecret)
    .update(payload)
    .digest('hex');

  const options = {
    url: completeURL,
    headers: {
      'X-BFX-APIKEY': apiKey,
      'X-BFX-PAYLOAD': payload,
      'X-BFX-SIGNATURE': signature
    },
    body: JSON.stringify(params),
    json: true
  };

  request.post(options, (error, response, res_body) => {

    console.log(error);
    console.log(res_body);

    if(error) {
      reject(error);
    }
    else {
      let parsed;
      try {
        parsed = res_body;
        if(parsed.message){
          reject(parsed);
        }
        else {
          resolve(parsed);
        }
      }
      catch(err) {
        reject(err);
      }
    }

  })

});


}