我想从Kraken.com API检索数据。我想打电话给'#34;私人"方法。 (那些需要进行身份验证)
在此精确:https://www.kraken.com/help/api
预期的签名是:
API-Sign =使用HMAC-SHA512的消息签名(URI路径+ SHA256(nonce + POST数据))和base64解码的秘密API密钥
我在这里找到了一个应该从他们的node.js库中执行该功能的函数,但我似乎无法得到它。
/**
* This method returns a signature for a request as a Base64-encoded string
* @param {String} path The relative URL path for the request
* @param {Object} request The POST body
* @param {Integer} nonce A unique, incrementing integer
* @return {String} The request signature
*/
function getMessageSignature(path, request, nonce) {
var message = querystring.stringify(request);
var secret = new Buffer(config.secret, 'base64');
var hash = new crypto.createHash('sha256');
var hmac = new crypto.createHmac('sha512', secret);
var hash_digest = hash.update(nonce + message).digest('binary');
var hmac_digest = hmac.update(path + hash_digest, 'binary').digest('base64');
return hmac_digest;
}
这是我的完整代码
function main() {
var apiKey = "API-KEY";
var apiSecret = "API-SECRET";
var url = "https://api.kraken.com/0/private/Balance";
var path = "/0/private/Balance";
const nonce = new Date() * 1000;
const payload = {
'nonce': nonce
};
const postData = 'nonce=' + nonce;
const signature = getMessageSignature(path, apiSecret, postData, nonce);
var httpOptions = {
'method': 'post',
'headers': {
"API-Key": apiKey,
"API-Sign": signature
},
'payload': postData
};
var response = UrlFetchApp.fetch(url, httpOptions);
Logger.log(response.getContentText());
}
function getMessageSignature(url, secret, data, nonce) {
const hash = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_256, nonce + data);
const hmac_digest = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, url + hash, Utilities.base64Decode(secret));
return Utilities.base64Encode(hmac_digest);
}
但我最终得到错误
{"error":["EAPI:Invalid key"]}
感谢您的期待。
答案 0 :(得分:1)
我终于使用jsSHA
获取了代码function getKrakenSignature (path, postdata, nonce) {
var sha256obj = new jsSHA ("SHA-256", "BYTES");
sha256obj.update (nonce + postdata);
var hash_digest = sha256obj.getHash ("BYTES");
var sha512obj = new jsSHA ("SHA-512", "BYTES");
sha512obj.setHMACKey ("HMACKEY", "B64");
sha512obj.update (path);
sha512obj.update (hash_digest);
return sha512obj.getHMAC ("B64");
}
function getKrakenBalance () {
var path = "/0/private/Balance";
var nonce = new Date () * 1000;
var postdata = "nonce=" + nonce;
var signature = getKrakenSignature (path, postdata, nonce);
var url = "https://api.kraken.com" + path;
var options = {
method: 'post',
headers: {
'API-Key': "<API-KEY>",
'API-Sign': signature
},
payload: postdata
};
var response = UrlFetchApp.fetch (url, options);
return response.getContentText ();
}
function main() {
Logger.log(getKrakenBalance());
}