简体   繁体   English

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

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

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. 在继续使用Google Apps脚本构建Google SpreadSheet的过程中,我已经完成了Bittrex和Poloniex的平衡,但无法使用Cryptopia。

Here is a link to my struggles with Bittrex Map JSON objects array to strings 这是我为Bittrex将JSON对象数组转换为字符串而奋斗的链接

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

Here are some examples: 这里有些例子:

  1. https://www.cryptopia.co.nz/Forum/Thread/262 https://www.cryptopia.co.nz/Forum/Thread/262
  2. https://github.com/Coac/cryptopia.js/blob/master/index.js https://github.com/Coac/cryptopia.js/blob/master/index.js
  3. https://github.com/sigwo/node-cryptopia/blob/master/cryptopia.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());
}

From your sample links, it seems that hmacsignature is encoded by base64. 从示例链接中,似乎hmacsignature是由base64编码的。 So how about the following modofication? 那么下面的修饰呢?

From : 来自:

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

To : 至 :

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

Note : 注意 :

  • Is nonce declared? 是否宣布nonce 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 and Content-Length . 所以我添加了这个和Content-Length And then, is secret encoded by base64? 然后, secret是由base64编码的吗? I thought that it may be encoded from other scripts. 我认为它可能是从其他脚本编码的。

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) method does not compute the binary input correctly. Utilities.computeHmacSignature(algorithm, value, key)方法无法正确计算二进制输入。 The type of value and key parameter is String , but Utilities.base64Decode 's result is Byte[] . valuekey参数的类型为String ,但Utilities.base64Decode的结果为Byte[] The raw binary value is changed while being converted from Byte[] to String . 原始二进制值在从Byte[]转换为String时被更改。

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

The following can be used to calculate the correct value, and fetch the result by proper UrlFetchApp.fetch 's options . 以下内容可用于计算正确的值,并通过适当的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