gpt4 book ai didi

javascript - Google 脚本 POST 方法与 poloniex api 调用的 JAVA 不匹配

转载 作者:行者123 更新时间:2023-12-02 13:17:34 26 4
gpt4 key购买 nike

我花了很多时间试图找出解决这个问题的方法,但没有成功。我相信这将是这个社区的糖果。问题:我正在尝试调用 poloniex.com 私有(private) API 函数。我设法在 JAVA Net beans 中做到这一点。这是一段代码:

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



}

如果有任何帮助,我将非常高兴。我相信这一定很简单,但我就是想不出来。非常感谢

最佳答案

给你。私有(private)和公共(public)通话的绝佳示例。

鸣谢: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);



};

关于javascript - Google 脚本 POST 方法与 poloniex api 调用的 JAVA 不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43705170/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com