簡體   English   中英

Google腳本POST方法與poloniex api調用的JAVA不匹配

[英]Google script POST method not matching JAVA for poloniex api call

我花了很多時間試圖弄清楚如何解決這個問題,但沒有成功。 我相信這將是這個社區的糖果。 問題:我正在嘗試調用poloniex.com私有API函數。 我設法在JAVA net bean中做到了這一點。 這是一段代碼:

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));

但是在Google腳本中,我總是收到錯誤響應:“無效命令”,我相信這是由於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);



}

我會很高興為您提供任何幫助。 我相信它必須很簡單,但我無法弄清楚。 非常感謝你

干得好。 私人和公共通話的典范。

積分: 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);



   };

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM