Shaun Scholtz
Shaun Scholtz

Reputation: 21

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 research on the documentation and done the following:

<?php
$host = "api.shiplogic.com";
$accessKey = '******';
$secretKey = '******';
$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": "17 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 guru assist me and point out if I am doing anything wrong?? Assistance would be highly appreciated.

P.S, I'm using sandbox credentials here, no worries.

Upvotes: 2

Views: 490

Answers (2)

Gavin Simpson
Gavin Simpson

Reputation: 2832

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%.

Upvotes: 1

Mark
Mark

Reputation: 637

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',
];

Upvotes: 1

Related Questions