frank1 has asked for the wisdom of the Perl Monks concerning the following question:

Am having a problem with my script

Am getting this error

{"label":"REQUEST_EXPIRED","message":"gap between request Timestamp and server time exceeds 60"} HTTP POST error code: 403 HTTP POST error message: Forbidden

This is the API shell example

key="YOUR_API_KEY" secret="YOUR_API_SECRET" host="https://api.gateio.ws" prefix="/api/v4" method="POST" url="/withdrawals" query_param="" body_param='{"withdraw_order_id":"order_123456","currency":"USDT","add +ress":"1HkxtBAMrA3tP5ENnYY2CZortjZvFDH5Cs","amount":"222.61","memo":" +","chain":"TRX"}' timestamp=$(date +%s) body_hash=$(printf "$body_param" | openssl sha512 | awk '{print $NF}') sign_string="$method\n$prefix$url\n$query_param\n$body_hash\n$timestam +p" sign=$(printf "$sign_string" | openssl sha512 -hmac "$secret" | awk '{ +print $NF}') full_url="$host$prefix$url" curl -X $method $full_url -d "$body_param" -H "Content-Type: applicati +on/json" \ -H "Timestamp: $timestamp" -H "KEY: $key" -H "SIGN: $sign"

This is my code

#!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; use JSON qw( encode_json ) use Digest::SHA qw(hmac_sha512_hex); use MIME::Base64; use POSIX qw( strftime ); my $apiKey = 'demo'; my $host = "https://api.gateio.ws"; my $prefix = "/api/v4"; my $url = '/withdrawals'; my $query_param = ''; my %mas = ( "amount" => "222.61", "currency" => "USDT", "address" => "1HkxtBAMrA3tP5ENnYY2CZortjZvFDH5Cs", "chain" => "TRX" ); my $body = encode_json \%mas; my $time = time; my $timestamp = strftime("%Y%m%d %S", localtime($time)); my $sign_headers = hmac_sha512_hex('POST', $prefix . $url, $query_para +m, $body, $timestamp); my %payload = ( "KEY" => $apiKey, "Timestamp" => $timestamp, "SIGN" => $sign_headers, "Content-Type" => 'application/json' ); my $ua = LWP::UserAgent->new; my $response = $ua->post( $host . $prefix . $url, %payload, Content => $body ); if ($response->is_success) { print $response->decoded_content; } else { print $response->decoded_content; print "HTTP POST error code: ", $response->code, "\n"; print "HTTP POST error message: ", $response->message, "\n"; }

I believe the problem is here

Example indicates

timestamp=$(date +%s)

And me am doing it this way

my $time = time; my $timestamp = strftime("%Y%m%d %S", localtime($time)); # prints example 20240924 55

Replies are listed 'Best First'.
Re: REQUEST EXPIRED
by Corion (Patriarch) on Sep 24, 2024 at 17:31 UTC

    Again, as always, compare what the working example does, and compare that with what your code does:

    date +%s

    This outputs on my machine:

    1727198961

    The Perl equivalent is:

    perl -wE "say time"

    ... which outputs:

    1727199064

    So, change your method of formatting the timestamp to:

    my $timestamp = time();

      Thanks it worked!

      But i need to read more about blank brackets what it does

      because i tried my $timestamp = time; and never works

      but with blank bracket am surprised that it worked

          because i tried my $timestamp = time; and never works

        It should

        perl -wE '$x=time; $y=time(); say $x; say $y'; date +%s 1727210259 1727210259 1727210259

Re: REQUEST EXPIRED
by marto (Cardinal) on Sep 24, 2024 at 16:42 UTC

    This tends to happen when your system isn't using clock isn't in sync with UTC. Are you syncing with ntpd?

    Update: Thank you for taking previous feedback into account, and making more effort when posting asking for help.

      am using shared hosting on pair Networks