May 20, 2022
0
Level 1
InvalidSignatureException Amazon sp api
Hello, I'm trying to connect the amazon SP api and I get this exception when making a request to the api:InvalidSignatureException I've looked on the amazon site and it says: "The authentication token in the request has expired. " But I can't seem to find where in my request I'm going wrong this is my code:
public function GetInfo($url, $options)
{
$uri = $url;
$apiURL = self::AMAZON_API_URL;
$marketplaceID = self::MARKETPLACE_ID;
$url = "$apiURL$url?marketplaceIds=$marketplaceID&granularityType=Marketplace&granularityId=$marketplaceID";
//datetime format: YYYY-MM-DDTHH:MM:SS.SSSZ
$accesKey = env('AMAZON_ACCESS_KEY');
$datetime = date("Ymd\THis\Z");
$date = date("Ymd", strtotime($datetime));
$this->Authenticate();
$signature = $this->GetSignature($datetime,$uri);
$authorizationString = "AWS4-HMAC-SHA256 Credential=$accesKey/$date/eu-west-1/execute-api/aws4_request, SignedHeaders=content-type;host;x-amz-date;x-amx-access-token, Signature=$signature";
if (!empty($options)) {
$request = Http::withHeaders([
'Accept' => 'application/json',
'host' => self::AMAZON_API_URL,
'user-agent' => 'bol-voorraad/1.0 (Language=PHP/7.2.0; platform=windows/10)',
'x-amz-access-token' => $this->token['access_token'],
'x-amz-date' => $datetime,
])->post($url, $options);
} else {
$request = Http::withHeaders([
'Authorization' => $authorizationString,
'content-type' => 'application/json',
'host' => 'sellingpartnerapi-eu.amazon.com',
'x-amz-date' => $datetime,
'x-amz-access-token' => $this->token['access_token'],
])->get($url);
}
dd($request);
return $this->DecodeBody($request);
}
public function GetSignature($datetime, $uri)
{
$secretKey = env("AMAZON_SECRET_KEY");
$date = date("Ymd", strtotime($datetime));
$region = self::REGION_STRING;
$service = "execute-api";
$query = "marketplaceIds=". self::MARKETPLACE_ID ."&granularityType=Marketplace&granularityId=". self::MARKETPLACE_ID;
$payload = hash("sha256", "");
$credentialScope = "$date/$region/$service/aws4_request";
//create a cannonical request
$canonicalRequest = "GET" . "\n" .
$uri . "\n" .
$query . "\n" .
"content-type:" . "application/json" . "\n" .
"host:" . self::AMAZON_API_URL . "\n" .
"x-amz-date:" . $datetime . "\n" .
"x-amz-access-token:" . $this->token['access_token'] . "\n" .
"\n" .
"content-type;host;x-amz-date;x-amz-access-token" . "\n" .
hash("sha256", "");
//create a string to sign
$stringToSign = "AWS4-HMAC-SHA256" . "\n" .
$datetime . "\n" .
$credentialScope . "\n" .
hash("sha256", utf8_encode($canonicalRequest));
$signingKey = $this->GetSignatureKey($secretKey, $datetime, $region, $service);
$signature = hash_hmac("sha256", $stringToSign, $signingKey);
return $signature;
}
public function GetSignatureKey($secretKey,$date,$region,$service)
{
$kSecret = "AWS4" . $secretKey;
$kDate = hash_hmac("sha256", $date,$kSecret, true);
$kRegion = hash_hmac("sha256", $region, $kDate, true);
$kService = hash_hmac("sha256", $service, $kRegion, true);
$kSigning = hash_hmac("sha256", "aws4_request", $kService, true);
return $kSigning;
}
public function GetInventory()
{
$url = '/fba/inventory/v1/summaries';
$options = null;
$body = $this->getInfo($url, $options);
}
Please or to participate in this conversation.