0

我花了很多时间试图弄清楚如何解决这个问题,但没有成功。我相信这将是这个社区的糖果。问题:我正在尝试调用 poloniex.com 私有 API 函数。我设法在 JAVA 网豆中做到了这一点。这是一段代码:

JAVA:

   String queryArgs = "command=returnBalances&nonce=" + nonce;
  System.out.println("queryArgs: " + queryArgs);  
    Mac shaMac = Mac.getInstance("HmacSHA512");
  System.out.println("shaMac: " + shaMac);  
    SecretKeySpec keySpec = new SecretKeySpec(secret.getBytes(), "HmacSHA512");
    shaMac.init(keySpec);
    final byte[] macData = shaMac.doFinal(queryArgs.getBytes());
  System.out.println("macData: " + macData);    
    String sign = Hex.encodeHexString(macData);
  System.out.println("sign: " + sign);    
    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("command", "returnTicker"));
    params.add(new BasicNameValuePair("nonce", nonce));
    post.setEntity(new UrlEncodedFormEntity(params));
    System.out.println("post: " + post.toString());  
    System.out.println("params: " + params);   
    CloseableHttpResponse response = httpClient.execute(post);
    HttpEntity responseEntity = response.getEntity();
    System.out.println(response.getStatusLine());
    System.out.println(EntityUtils.toString(responseEntity));

但在谷歌脚本中我总是得到错误响应:“无效命令”我相信这是由于错误的 POST 格式。非工作代码在这里:

google script:

function returnBalances() {

  var nonce = generateNonce().toString();
  var queryArgs = "command=returnBalances&nonce=" + nonce;
  var sign = signKey(queryArgs, api_secret);

Logger.log("final api_key:" + api_key);     
Logger.log("final sign:" + sign);   

   var headers = {
   "Key" : api_key,
   "Sign" : sign,
    };

   var      options = {
     "contentType" : "application/json",
     "method" : "POST",
     "headers" : headers,
     {"command": "returnBalances",
       nonce": 20000},
       };

    Logger.log("headers"+JSON.stringify(headers));
    Logger.log("options"+JSON.stringify(options));


  var result = JSON.parse(UrlFetchApp.fetch(trading_url,  options).getContentText());
  Logger.log(result);
  Logger.log(options);



}

我会很高兴得到任何帮助。我相信它一定很简单,但我就是想不通。非常感谢

4

1 回答 1

1

干得好。私人电话和公共电话的绝佳示例。

学分:https ://pastebin.com/TXB7Ed7W

// This is to use Google spreadsheet and Google Script with the Poloniex API both using public calls (GET) than private calls (POST) --> BTC donation: 122gvDXaLNLHyaYuMk9jh7fHXcRAffM6D5  ETH donation: 0x92e4A8627455c4069d6A963B793CE64013772Fb6

function updatePoloniex()
{
  // TODO: This function is using the GET method and it's for the public api so you won't need login.
  // Choose the command you want from https://poloniex.com/support/api/, in my case is returnTicker
  var response = UrlFetchApp.fetch("https://poloniex.com/public?command=returnTicker");
  // TODO: set your sheet name here
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("DATA");
  // The following line will parse in the object 'json' the result of 'returnTicker' which has previously been put into the variable 'response'
  var json = JSON.parse(response.getContentText());
  // next, we ask to create a variable for each of the ticker we want to look for
  var rate = json.BTC_MAID.last;
  var rate2 = json.BTC_DAO.last;
  var rate3 = json.BTC_LSK.last;
  var rate4 = json.BTC_ETH.last;
  var rate5 = json.ETH_DAO.last;
  var rate6 = json.ETH_LSK.last;
  var rate7 = json.USDT_ETH.last;

  // then we put the result of the parsing into the coordinates we desire on the sheet
  // TODO: set column coordinates here in format (column, row); this is now set to A1
  sheet.getRange(1, 1).setValue(rate);
  sheet.getRange(2, 1).setValue(rate2);
  sheet.getRange(3, 1).setValue(rate3);
  sheet.getRange(4, 1).setValue(rate4);
  sheet.getRange(5, 1).setValue(rate5);
  sheet.getRange(6, 1).setValue(rate6);
  sheet.getRange(7, 1).setValue(rate7);
};

  // THIS IS THE PART THAT SHOWS YOU HOW TO DO PRIVATE CALLS with the POST METHOD and your Poloniex secret key 
function sendHttpPost() {

  // First you create a nonce, which is an incremental random number, to do so, we can use the current date time summed with a number
  var nonce = 1465426902234426 + new Date().getTime();

  // First choose a command from here https://poloniex.com/support/api/ seeing if it require specific options
  // Then, we set the variable 'p' as a string which combine the command & any related parameter (if any) & the nonce.
  // in this case we specified the account=all parameter for the command=returnCompleteBalances as it gives us also loans and on orders balances
  var p = "command=returnCompleteBalances&account=all&nonce="+nonce

  // Then, we sign this variable 'p' with our secret key (taken from the API of Poloniex) to obtain a new string 'signature'
   var signature = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512,
                                                p,
                                                "HERE YOU PUT YOUR SECRET KEY");

  // Then, we convert the resulting string: from an array of byte (which is a standard output) to HEX
  signature = signature.map(function(byte) {
    return ('0' + (byte & 0xFF).toString(16)).slice(-2);
  }).join('')

  // Then we create the variable 'headers' and we specify in the object "Key" the API key associated with the secret key (always taken from the API of Poloniex), and we pass the 'signature' output string to the object "Sign" 
  var headers = {
    "Key" : "HERE YOU PUT YOUR API KEY",
    "Sign" : signature
  };

    // then we define 'options' as POST method, specifying the headers and the payload
   var options = {
     "method" : "POST",
     "headers": headers,
     "payload": p
   };


  // then we fetch the url passing the 'options' which make us call the command and sign it
  var response2 = UrlFetchApp.fetch("https://poloniex.com/tradingApi", options);

  // we decide in which sheet of the current Google spreadsheet doc we want to dump the results
   var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("DATA");

  // then we parse the fetched url in the var 'json2'
   var json2 = JSON.parse(response2.getContentText());

  // asking different values that we pass into specific variables
   var btcbalance = json2.BTC;
   var maidbalance = json2.MAID;
   var daobalance = json2.DAO;
   var lskbalance = json2.LSK;
   var ethbalance = json2.ETH;
  Logger.log(btcbalance);

  // since each currency provide three balances onOrders, available, btcValue in a single string but we want them in single cell of the sheet we take them providing them a home :^)

    sheet.getRange(1, 3).setValue(btcbalance.onOrders);
    sheet.getRange(1, 4).setValue(btcbalance.available);
    sheet.getRange(1, 5).setValue(btcbalance.btcValue);

    sheet.getRange(2, 3).setValue(maidbalance.onOrders);
    sheet.getRange(2, 4).setValue(maidbalance.available);
    sheet.getRange(2, 5).setValue(maidbalance.btcValue);

    sheet.getRange(3, 3).setValue(daobalance.onOrders);
    sheet.getRange(3, 4).setValue(daobalance.available);
    sheet.getRange(3, 5).setValue(daobalance.btcValue);

    sheet.getRange(4, 3).setValue(lskbalance.onOrders);
    sheet.getRange(4, 4).setValue(lskbalance.available);
    sheet.getRange(4, 5).setValue(lskbalance.btcValue);

    sheet.getRange(5, 3).setValue(ethbalance.onOrders);
    sheet.getRange(5, 4).setValue(ethbalance.available);
    sheet.getRange(5, 5).setValue(ethbalance.btcValue);



   };
于 2017-05-22T23:44:58.037 回答