繁体   English   中英

Google表格中的Cryptopia API(Google Apps脚本)

[英]Cryptopia API in Google Sheets (Google Apps Script)

在继续使用Google Apps脚本构建Google SpreadSheet的过程中,我已经完成了Bittrex和Poloniex的平衡,但无法使用Cryptopia。

这是我为Bittrex将JSON对象数组转换为字符串而奋斗的链接

这是一个官方的API链接: https : //www.cryptopia.co.nz/Forum/Thread/256

这里有些例子:

  1. https://www.cryptopia.co.nz/Forum/Thread/262
  2. https://github.com/Coac/cryptopia.js/blob/master/index.js
  3. https://github.com/sigwo/node-cryptopia/blob/master/cryptopia.js

这是我的代码,其中显示“无效授权标头”错误:

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

从示例链接中,似乎hmacsignature是由base64编码的。 那么下面的修饰呢?

来自:

var hmacsignature = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_256,signature,secret);

至 :

var hmacsignature = Utilities.base64Encode(Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_256,signature,secret));

注意 :

  • 是否宣布nonce 如果尚未声明,则可以使用以下脚本。
    • var nonce = Math.floor(new Date().getTime() / 1000);

我无法测试。 所以我不知道这是否行得通。 如果这不起作用,对不起。

编辑:

这个怎么样? var params = {}; 即使没有请求参数也可能是必需的。 所以我添加了这个和Content-Length 然后, secret是由base64编码的吗? 我认为它可能是从其他脚本编码的。

var key = keys.getRange("B4").getValue();
var secret = keys.getRange("C4").getValue();
var nonce = Math.floor(new Date().getTime() / 1000); // Added
var params = {}; // Added
var baseUrl = 'https://www.cryptopia.co.nz/api/';
var command = "GetBalance";
var url = baseUrl + command;
var requestContentBase64String = Utilities.base64Encode(Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, JSON.stringify(params), Utilities.Charset.UTF_8)); // Added
var signature = key + "POST" + encodeURIComponent(url).toLowerCase() + nonce + requestContentBase64String; // Modified
var hmacsignature = Utilities.base64Encode(Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_256, signature, Utilities.base64Decode(secret), Utilities.Charset.UTF_8)); // Modified
var header_value = "amx " + key + ":" + 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
};
var response = UrlFetchApp.fetch(url, options);
var json = JSON.parse(response.getContentText());

Utilities.computeHmacSignature(algorithm, value, key)方法无法正确计算二进制输入。 valuekey参数的类型为String ,但Utilities.base64Decode的结果为Byte[] 原始二进制值在从Byte[]转换为String时被更改。

使用jsSHA和cf。 https://stackoverflow.com/a/14007167

以下内容可用于计算正确的值,并通过适当的UrlFetchApp.fetchoptions获取结果。

.... paste src/sha256.js contents ...

...  
var params = {"Currency" : "BTC"};
...
var sha = new jsSHA("SHA-256", "TEXT");
sha.setHMACKey(secret, "B64");
sha.update(signature);
var hmacsignature = sha.getHMAC("B64");

var header_value = "amx " + key + ":" + hmacsignature + ":" + nonce;
var headers = {
  "Authorization" : header_value,
};

var options = {
  "contentType": 'application/json; charset=utf-8',
  "method": 'post',
  "headers": headers,
  "payload": JSON.stringify(params),
  "contentLength": JSON.stringify(params).length
};

var response = UrlFetchApp.fetch(url, options);
var json = JSON.parse(response.getContentText());

Logger.log(json);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM