在perl中处理Bitfinex身份验证

时间:2017-06-29 03:21:10

标签: javascript perl api

我正在尝试使用Bitfinex API在perl中验证自己。但是,无论我做什么,

HTTP POST error code: 400
HTTP POST error message: Bad Request

示例代码在javascript中给出,如下所示,

const request = require('request')
const crypto = require('crypto')

const apiKey = '<Your API key here>'
const apiSecret = '<Your API secret here>'
const baseUrl = 'https://api.bitfinex.com'

const url = '/v1/account_infos'
const nonce = Date.now().toString()
const completeURL = baseUrl + url
const body = {
  request: url,
  nonce
}
const payload = new Buffer(JSON.stringify(body))
    .toString('base64')

const signature = crypto
  .createHmac('sha384', apiSecret)
  .update(payload)
  .digest('hex')

const options = {
  url: completeURL,
  headers: {
    'X-BFX-APIKEY': apiKey,
    'X-BFX-PAYLOAD': payload,
    'X-BFX-SIGNATURE': signature
  },
  body: JSON.stringify(body)
}

return request.post(
  options,
  function(error, response, body) {
    console.log('response:', JSON.stringify(body, 0, 2))
  }
)

我在perl中尝试了以下内容,

  my $nonce=int(1000*time());
  my $baseurl="https://api.bitfinex.com";
  my $url="/v1/account_infos";
  my $completeurl=$baseurl . $url;
  my $body=[$url, $nonce];
  my $message=to_json($body);
  my $payload = encode_base64($message);
  my $sig = hmac_sha384_hex($payload,$apiSecret);
  my $ua = LWP::UserAgent->new();
  my $req = POST($completeurl, ['X-BFX-APIKEY' => $apiKey, 'X-BFX-PAYLOAD' => $payload, 'X-BFX-SIGNATURE' => $sig]);
  my $resp = $ua->request($req);
  if ($resp->is_success) {
    print "success\n";

  }
  else {
    print "HTTP POST error code: ", $resp->code, "\n";
    print "HTTP POST error message: ", $resp->message, "\n";

  }

有人可以指出我的(很多)错误吗?非常感谢。

1 个答案:

答案 0 :(得分:1)

JavaScript示例代码中存在拼写错误。

而不是

<input type="text" name="xstartdate" class="form-control input-sm" id="mystartdate" placeholder="Required">
<input type="text" name="xenddate" class="form-control input-sm" id="myenddate" placeholder="Required">
<input type="text" id="num_nights" readonly>

应该有

const body = {
  request: url,
  nonce
}

所以,Perl版本应该有

const body = {
  'request': url,
  'nonce': nonce
}