3

I am trying to connect to Cryptopia API to get my balance in Google Sheet and I can't make it work. The error I am currently getting is:

Signature does not match request parameters

API Private Documentation

My code:

  var key1 = "API_KEY";
  var secret1 = "API_SECRET";
  var nonce = Math.floor(new Date().getTime()/1000);  
  var params = {"Currency" : "BTC"};

  var url = 'https://www.cryptopia.co.nz/api/GetBalance';
  var requestContentBase64String = Utilities.base64Encode(Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, JSON.stringify(params), Utilities.Charset.UTF_8)); // Added
  var signature = key1 + "POST" + encodeURIComponent(url).toLowerCase() + nonce + requestContentBase64String; // Modified
  var hmacsignature = Utilities.base64Encode(Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_256, signature, Utilities.base64Decode(secret1), Utilities.Charset.UTF_8)); // Modified
  var header_value = "amx " + key1 + ":" + hmacsignature + ":" + nonce;
  var headers = {
    "Authorization": header_value,
    "Content-Type": 'application/json; charset=utf-8'
    //"Content-Length" : Utilities.newBlob(JSON.stringify(params)).getBytes().length // Added
  };
  var options = {
    "method": 'POST',
    "headers": headers,
    "payload": JSON.stringify(params),
    "contentLength": JSON.stringify(params).length
  };
  var response = UrlFetchApp.fetch(url, options);
  Logger.log(response)
  return JSON.parse(response.getContentText());

There is one more thread regarding the same issue on stackoverflow but without correct answer.

Thank you

Petr
  • 1,853
  • 2
  • 25
  • 49

0 Answers0