2017-06-29 5 views
-2

Ich versuche BitFinex API zu verwenden, um mich in Perl zu authentifizieren. Aber was immer ich tue ich,Handle Bitfinex Authentifizierung in Perl

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

Der Beispielcode wird wie unten in Javascript gegeben,

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)) 
    } 
) 

ich in Perl folgend versucht,

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"; 

    } 

Kann jemand darauf hinweisen, meine (viele) Fehler? Danke vielmals.

Antwort

1

Im JavaScript-Beispielcode ist ein Tippfehler enthalten.

Statt

const body = { 
    request: url, 
    nonce 
} 

sollte es

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

So sollte die Perl-Version haben

my $body = { request => $url, nonce => $nonce }; 
Verwandte Themen