1

Okay I have been beating my head against wall trying to get Cryptopia data to Google spreadsheet using API for several days - any help greatly appreciated.

Response is always {Error=Signature does not match request parameters., Success=false}

I have been using CryptoJS for my crypto MD5, SHA-256, and Base64. Here's some references I have been using:

Similar Stackoverflow issue - final answer not working

Cryptopia Documentation

Github

Here's my code :

..........

Crypto JS MD5 Rollup Script, hmac-sha256 rollup script, and enc-Base64 script

..........

var key = API_KEY;
var secret = API_SECRET;

var uri =  'https://www.cryptopia.co.nz/api/getbalance';
var nonce = Math.floor(new Date().getTime() / 1000);

var post_parameters={'Currency': 'BTC'};
var param = JSON.stringify(post_parameters);
var param_hash = CryptoJS.MD5(param).toString(CryptoJS.enc.Base64);

var reqSignature = key + "POST" + uri + nonce + param_hash;

var hmacsignature = CryptoJS.HmacSHA256(reqSignature,secret).toString(CryptoJS.enc.Base64);

var AUTH = "amx " + key + ":" + hmacsignature + ":" + nonce;

var headers = {
  'Authorization': AUTH,
};

var options = {
  'contentType': 'application/json; charset=utf-8',
  'method': 'post',
  'headers': headers,
  'payload' : param,
  'contentLength': param.length
};

var balance_json = UrlFetchApp.fetch('https://www.cryptopia.co.nz/api/getbalance', options);

var balance = JSON.parse(balance_json);
Logger.log(balance);

}

I managed to get the code working using the following github which has many great API examples!: https://github.com/ManuCart/Cryptocurrency-Portfolio

function Cryptopia () {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Config");
  var key = sheet.getRange("B18").getValue()
  var secret = sheet.getRange("B19").getValue();
  var digest = "{}";

var rawHash = Utilities.base64Encode(Utilities.computeDigest(Utilities.DigestAlgorithm.MD5,digest));
//Logger.log(rawHash+"  "+rawHash);

url="https://www.cryptopia.co.nz/Api/GetBalance"
var url_encoded = encodeURIComponent(url).toLowerCase()
//Logger.log(url_encoded);

var nonce = Math.floor(new Date().getTime()/1000);

signature=key+"POST"+url_encoded+nonce+rawHash
//Logger.log("signature "+signature);

var shaObj = new jsSHA("SHA-256", "TEXT");
shaObj.setHMACKey(secret, "B64");
shaObj.update(signature);
var hmac = shaObj.getHMAC("B64");

//Logger.log("signed: "+hmac);

header_value="amx "+key+":"+hmac+":"+nonce
Logger.log("header_value: "+header_value);
var options = {
method: 'POST',
headers: {
     'Content-Type': 'application/json; charset=utf-8',
     'Authorization': header_value },
payload: '{}'
     }

var response = UrlFetchApp.fetch (url, options);
var data = JSON.parse(response.getContentText());
//Logger.log(data)  


  var array = [];

  //{Error=null, Data=[{Status=OK, Address=null, HeldForTrades=0, 
Symbol=1337, Unconfirmed=0, PendingWithdraw=0, Total=0, Available=0, 
BaseAddress=null, CurrencyId=331, StatusMessage=null}, {Status=OK, 
Address=null, HeldForTrades=0, Symbol=21M, Unconfirmed=0, PendingWithdraw=0, 
Total=0, Available=0, BaseAddress=, CurrencyId=573, StatusMessage=null}
  for(var x in data.Data){ balance=parseFloat(data.Data[x].Total);
                               if (balance > 0) {
                                 asset=data.Data[x].Symbol
                                 array.push({'currency': asset, 'balance': 
balance, 'market': "Cryptopia"})
                           } 
                         }
  //Logger.log(array);
  return array;
}
  • Are you sure you are using the right api key/secret? (I know it's basic but double checking it is aways usefull) – Liora Haydont Feb 28 '18 at 17:29
  • 1
    Apps Script has a built in Utilities class with similar methods and functionality when compared to CryptoJS. Maybe you'll have better luck if you used the native utilities instead. – TheAddonDepot Feb 28 '18 at 18:00
  • Also found this thread that may help you: https://www.cryptopia.co.nz/Forum/Thread/262 – TheAddonDepot Feb 28 '18 at 18:10
  • I have used ComputeHmacSha256Signature and base64Encode under Utilities. Yeah non of that works. I feel like it might be something simple I am just at a loss at this point. – Andrew Wallace Feb 28 '18 at 19:18
  • I did notice other Java scripts were passing the Bytelength of the param_hash and not the string.length, wonder if this is problem? – Andrew Wallace Feb 28 '18 at 19:23
  • Related: https://stackoverflow.com/a/63647410/ – TheMaster Aug 29 '20 at 13:30

0 Answers0