in reply to POST API in Perl using LWP::UserAgent with authentication providing errors

2 problems I can see so far

1. your urlencode() is escaping dots. As hippo said use a module. See the difference

#!perl use strict; use URI::Escape; my $url = "https://www.cryptopia.co.nz/api/GetBalance"; print uri_escape($url)."\n"; print urlencode($url)."\n"; sub urlencode { my $s = shift; $s =~ s/ /+/g; $s =~ s/([^A-Za-z0-9\+-])/sprintf("%%%02X", ord($1))/seg; return $s; }

2. With MIME::Base64 encode_base64( $bytes ) by default adds line endings.
Try using encode_base64( $bytes,'' )

poj

Replies are listed 'Best First'.
Re^2: POST API in Perl using LWP::UserAgent with authentication providing errors
by ssara (Acolyte) on Jan 20, 2018 at 13:48 UTC

    I also tried using

    my $post_data_enc = encode_base64(md5($post_data),'');

    still it does not work!!

      When I run the node.JS code below I get a header value of

      amx PUBLIC KEY:leFWRGAm2eH6rm9t8hoHifpMpWPV5xMZcXtuhrQbUP0=:1516453770998

      Try the same values for keys and nonce in your code and compare

      var nonce = 1516453770998; var API_KEY = 'PUBLIC KEY' var API_SECRET = 'PRIVATE KEY'; var md5 = crypto.createHash('md5').update( JSON.stringify( {'Currency' +:"PAC"} ) ).digest(); var requestContentBase64String = md5.toString('base64'); var signature = API_KEY + "POST" + encodeURIComponent( 'https://www.cr +yptopia.co.nz/api/GetBalance' ).toLowerCase() + nonce + requestConten +tBase64String; var hmacsignature = crypto.createHmac('sha256', new Buffer( API_SECRET +, "base64" ) ).update( signature ).digest().toString('base64'); var header_value = "amx " + API_KEY + ":" + hmacsignature + ":" + nonc +e;
      poj