干得好。私人电话和公共电话的绝佳示例。
学分: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);
};