1

I am trying to get my account info from Binance.com with the following Perl code:

#!/usr/bin/perl

use strict;
use warnings;

use Digest::SHA qw(hmac_sha256_hex);
use Time::HiRes qw(time);

my $api_key = "X";
my $api_secret = "X";

my $data = "recvWindow=2000&timestamp=" . int(time() * 1000);
my $signature = uc(hmac_sha256_hex($data, $api_secret));

print `curl -s -m 3 -H 'X-MBX-APIKEY: $api_key' -d '$data&signature=$signature' -X GET 'https://api.binance.com/api/v3/account'` . "\n";

That code looks correct and should work, but I get the following error:

{"code":-1102,"msg":"Mandatory parameter 'timestamp' was not sent, was empty/null, or malformed."}

Ofcourse, the timestamp parameter is sent and not empty or null, nor malformed.

If I print the output to console, it shows the following:

curl -s -m 3 -H 'X-MBX-APIKEY: X' -d 'recvWindow=2000&timestamp=1516082731909&signature=X' -X GET 'https://api.binance.com/api/v3/account'

Can someone help please? Thank you.

References:

  1. Binance API official documentation / SIGNED Endpoint Examples
  2. Account information

Note: I replaced API Key/Secret and Signature with 'X'

Rahman
  • 21
  • 1
  • 1
  • 3

1 Answers1

3

For GET endpoints, parameters must be sent as a query string.

Basically, the point of using GET is to allow the response to be cached, and having to deal with a request body would make that needlessly complicated. Therefore, the body of GET requests should always be ignored, so -d never makes sense for a GET request.

You can properly form the URL as follows:

use URI qw( );

my $url = URI->new('https://api.binance.com/api/v3/account');
$url->query_form(
   recvWindow => 2000
   timestamp  => int(time() * 1000),
   signature  => uc(hmac_sha256_hex($data, $api_secret)),
);

You didn't just build the parameters incorrectly. You also had code injection bugs. You can properly form the shell command as follows:

use String::ShellQuote qw( shell_quote );

my $cmd = shell_quote(
   'curl',
   '-s',
   '-m' => 3,
   '-H' => 'X-MBX-APIKEY: $api_key',
   '-X' => 'GET',
   $url,
);
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • Correct. Missed that (https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md#general-api-information). Thank you. – Rahman Jan 16 '18 at 07:38
  • Thank you for the suggestion. Will re-do everything once the project is finished. – Rahman Jan 16 '18 at 08:06