I've spent many hours trying to figure out how to solve this problem but no success. I believe this will be a candy for this community. The problem: I'm trying to call poloniex.com private API functions. I managed to do this in JAVA net beans. This is the piece of code:
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));
but in google script I'm getting always error response : "invalid command" I believe it is due to bad POST formating. The nonworking code is here:
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);
}
I will be very happy for any help. I believe it must be simple but I just can't figure it out. Thank you very much