我正在尝试使用Perl将信息发送到API。我想打电话给the below API,这需要以下输入:
货币:(可选)
要返还的余额的货币符号,例如' DOT'
CurrencyId :(可选)
要返回的余额的Cryptopia货币标识符,例如' 2' (如果提供货币'则不需要。)
请求结构:
REQUEST_SIGNATURE:API_KEY +" POST" + URI + NONCE + HASHED_POST_PARAMS
API_KEY
您的Cryptopia API密钥
URI
请求URI。例如https://www.cryptopia.co.nz/api/GetBalance
HASHED_POST_PARAMS:
Post64参数的Base64编码MD5哈希
NONCE
每个请求的唯一指标。
以下是我的代码
use LWP::UserAgent;
use JSON;
use Digest::MD5 qw(md5);
use Digest::SHA qw(hmac_sha256_base64);
use MIME::Base64;
my $api_key = 'PUBLIC KEY';
my $api_private_key = 'PRIVATE KEY';
my $ua = LWP::UserAgent->new;
my $url = "https://www.cryptopia.co.nz/api/GetBalance";
my %req = (
Currency => "DOT"
);
my $nonce = int(rand(1000000));
my $post_data = encode_json(\%req);
my $post_data_enc = encode_base64(md5($post_data));
my $req_signature = sprintf("%sPOST%s%s%s", $api_key, lc(urlencode($url)), $nonce, $post_data_enc);
my $req_signature_hmac = hmac_sha256_base64($req_signature, decode_base64($api_private_key));
my $auth_header_value = sprintf("amx %s:%s:%s", $api_key, $req_signature_hmac, $nonce);
my $response = $ua->post($url,
Content => $post_data,
'Content-Type' => 'application/json',
'charset' => 'utf-8',
Authorization => $auth_header_value
);
die "Request failed: ", $response->content unless $response->is_success();
print $response->content, $/;
sub urlencode {
my $s = shift;
$s =~ s/ /+/g;
$s =~ s/([^A-Za-z0-9\+-])/sprintf("%%%02X", ord($1))/seg;
return $s;
}
此代码在JSON有效内容中提供以下错误
{"成功":false,"错误":"签名与请求参数不匹配。"}
我还检查了PUBLIC和PRIVATE密钥,它在我使用Java或PHP代码时有效。但是,当我使用Perl时,它只是给了我一个错误。请你能帮帮我吗?