Google表格中的Cryptopia API - 签名与请求参数不匹配

时间:2018-01-17 08:45:56

标签: javascript api google-sheets

我正在尝试连接到Cryptopia API以获得Google表格中的余额,但我无法使其正常运行。我目前得到的错误是:

Signature does not match request parameters

API Private Documentation

我的代码:

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

还有一个thread关于stackoverflow上的相同问题,但没有正确的答案。

谢谢

0 个答案:

没有答案