Google表格中的Cryptopia API(Google Apps脚本) [英] Cryptopia API in Google Sheets (Google Apps Script)

查看:283
本文介绍了Google表格中的Cryptopia API(Google Apps脚本)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继续使用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

这里是我的代码,它会得到Invalid authorization header错误:

  / /获取Cryptopia余额
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 +命令;

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


解决方案

Utilities.computeHmacSignature(算法,值,键)方法不会正确计算二进制输入。 参数的类型是 String ,但 Utilities.base64Decode 的结果是 Byte [] 。原始二进制值在从 Byte [] 转换为字符串时进行了更改。



使用 jsSHA ,以及参阅 https://stackoverflow.com/a/14007167



可以使用以下方法计算正确的值,并通过正确 UrlFetchApp.fetch 选项获取结果。

  ....粘贴src / sha256.js内容... 

...
var params = {Currency:BTC};
...
var sha = new jsSHA(SHA-256,TEXT);
sha.setHMACKey(秘密,B64);
sha.update(签名);
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);


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.

Here is a link to my struggles with Bittrex Map JSON objects array to strings

Here is an official API links: https://www.cryptopia.co.nz/Forum/Thread/256

Here are some examples:

  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

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

解决方案

Utilities.computeHmacSignature(algorithm, value, key) method does not compute the binary input correctly. The type of value and key parameter is String, but Utilities.base64Decode's result is Byte[]. The raw binary value is changed while being converted from Byte[] to String.

Use jsSHA, and cf. 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.

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

这篇关于Google表格中的Cryptopia API(Google Apps脚本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆