简体   繁体   中英

Generating a AWS Signature (ShipLogic)

So I'm implementing ship-logic's API into my program and have never worked with AWS authentication. Did my researched on the documentation and done the following:

<?php
$host = "api.shiplogic.com";
$accessKey = 'AKIA55D5DNTBFC3I7BHQ';
$secretKey = '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';
$requestUrl = 'https://api.shiplogic.com';
$uri = '/rates';
$httpRequestMethod = 'POST';
$data = '{"collection_address": {"company": "Kenesis Test","street_address": " 32 Goud Street, Goedeburg, Benoni","local_area": "Benoni","city": "Johannesburg","country": "ZA","code": "1501"},"delivery_address": {"street_address": "10 bloomberg street","local_area": "minnebron","city": "brakpan","code": "1541"},"parcels": [{"submitted_length_cm": 1,"submitted_width_cm": 1,"submitted_height_cm": 1,"submitted_weight_kg": 0.1}],"declared_value": 99}';;

require 'vendor/autoload.php';
use Aws\Signature\SignatureV4;
use Aws\Credentials\Credentials;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use Psr\Http\Client\ClientInterface;

$signature = new SignatureV4('execute-api', 'af-south-1');
$credentials = new Credentials($accessKeyId, $secretAccessKey);
$psr7Request = new Request($httpRequestMethod, $requestUrl.$uri, ["content-type"=>"application/json"], $data);
$client = new Client([$requestUrl, 'timeout' => 30]);
$sr = $signature->signRequest($psr7Request, $credentials);
$response = $client->send($sr);

//var_dump($response);
?>

Now its a simple piece of code but I'm constantly getting a 403 response. When I do the exact same request with PostMan, the response came back successfully.

Can any AWS gurru assist me and point out if I am doing anything wrong code wise?? Assistance would be highly appreciated.

PS, I'm using sandbox credentials here, no worries.

It's a headers issue. You need to include the following headers when sending the request through.

$amzDate = date('Ymd\THis\Z');
$headers = [
'X-Amz-Date'   => $amzDate,
'Cookie'       => 'XDEBUG_SESSION=PHPSTORM',
'Content-Type' => 'application/json',
];

There is an error in your variable names.

$credentials = new Credentials($accessKeyId, $secretAccessKey);

should be

$credentials = new Credentials($accessKey, $secretKey);

Once you fix those the code works 100%.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM