In continuation of building Google SpreadSheet using Google Apps Script I've done with getting my Bittrex and Poloniex balances, but can't get to work with Cryptopia.
Here is a link to my struggles with Bittrex Map JSON objects array to strings
Here is an official API links: https://www.cryptopia.co.nz/Forum/Thread/256
Here are some examples:
- https://www.cryptopia.co.nz/Forum/Thread/262
- https://github.com/Coac/cryptopia.js/blob/master/index.js
- https://github.com/sigwo/node-cryptopia/blob/master/cryptopia.js
Here is my code, which getting "Invalid authorization header" error:
// Get Cryptopia balances
var key = keys.getRange("B4").getValue();
var secret = keys.getRange("C4").getValue();
var baseUrl = 'https://www.cryptopia.co.nz/api/';
var command = "GetBalance";
var url = baseUrl + command;
var signature = key + "POST" + encodeURIComponent(url).toLowerCase() + nonce;
var hmacsignature = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_256,signature,secret);
var header_value = "amx " + key + ":" + hmacsignature + ":" + nonce;
var headers = { 'Authorization': header_value, 'Content-Type':'application/json; charset=utf-8' };
var options = {
method: 'POST',
headers: headers
};
var response = UrlFetchApp.fetch("https://www.cryptopia.co.nz/api/GetBalance", options);
var json = JSON.parse(response.getContentText());
}
Utilities.computeHmacSignature(algorithm, value, key)
method does not compute the binary input correctly. The type ofvalue
andkey
parameter isString
, butUtilities.base64Decode
's result isByte[]
. The raw binary value is changed while being converted fromByte[]
toString
.Use jsSHA, and cf. https://stackoverflow.com/a/14007167.
The following can be used to calculate the correct value, and fetch the result by proper
UrlFetchApp.fetch
'soptions
.From your sample links, it seems that
hmacsignature
is encoded by base64. So how about the following modofication?From :
To :
Note :
nonce
declared? If you have not declared it, you can use a following script.var nonce = Math.floor(new Date().getTime() / 1000);
I cannot test this. So I don't know whether this works fine. If this didn't work, I'm sorry.
Edit :
How about this?
var params = {};
might be required, even if there is no request parameters. So I added this andContent-Length
. And then, issecret
encoded by base64? I thought that it may be encoded from other scripts.