我也很喜欢其他很多人很难将Matlab与Kraken从混合来源联系起来,而且还没有明确的答案。希望有人可以澄清可能是类型错误,导致“EAPI:无效签名”错误,请简单说明。 (我不是Java程序员。)
uri='0/private/Balance';
postdata= '';
[response, status] = krakenAuthenticated(uri, postdata);
function [response,status]=krakenAuthenticated(uri,postdata)
url=['https://api.kraken.com/',uri];
% nonce
nonce = num2str(floor((now-datenum('1970', 'yyyy'))*8640000000));
key = ''; % Public key
secret = ''; % Private key
% 1st hash
Opt.Method = 'SHA-256';
Opt.Input = 'ascii';
sha256string = DataHash(['nonce=',nonce,postdata],Opt);
% 2nd hash
sign = crypto([uri, sha256string], matlab.net.base64decode(secret), 'HmacSHA512');
header_1=http_createHeader('API-Key',key);
header_2=http_createHeader('API-Sign',char(sign));
header=[header_1 header_2];
[response,status] = urlread2(url,'POST',['nonce=',nonce,postdata],header);
end
function header = http_createHeader(name,value)
header = struct('name',name,'value',value);
end
function signStr = crypto(str, key, algorithm)
import java.net.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import org.apache.commons.codec.binary.*
keyStr = java.lang.String(key);
key = SecretKeySpec(keyStr.getBytes('UTF-8'), algorithm);
mac = Mac.getInstance(algorithm);
mac.init(key);
toSignStr = java.lang.String(str);
signStr = java.lang.String(Hex.encodeHex( mac.doFinal( toSignStr.getBytes('UTF-8'))));
end