double-break / spapi-php

Amazon Selling Partner API PHP Client
MIT License
56 stars 48 forks source link

with the same code, the request signature we calculated does not match the signature you provided? #39

Closed yshengwu closed 3 years ago

yshengwu commented 3 years ago

On Windows 10, with the same code, I get the order normally with getOrder(), but I get an error with getOrders()

code: ..... //Create the request signer which will be automatically used to sign all of the //requests to the API $signer = new DoubleBreak\Spapi\Signer();

//Create Credentials service and call getCredentials() to obtain //all the tokens needed under the hood

$credentials = new DoubleBreak\Spapi\Credentials($tokenStorage, $signer, $config); $cred = $credentials->getCredentials();

$spapi = new DoubleBreak\Spapi\Api\Orders($cred, $config); / $order_id = '113-3351006-381xx02'; $response = $spapi->getOrder($order_id); print_r($response);exit; /

$params = [ 'MarketplaceIds' => ['ATVPDKIKX0DER'], / 'LastUpdatedAfter'=> '2021-06-02T09:46:28Z', / ]; $response = $spapi->getOrders($params); print_r($response);exit;

error info: Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: GET https://sellingpartnerapi-na.amazon.com/orders/v0/orders?MarketplaceIds%5B0%5D=ATVPDKIKX0DER resulted in a 403 Forbidden response: { "errors": [ { "message": "The request signature we calculated does not match the signature you provided. C (truncated...)

aniruddhaad commented 3 years ago

read my question - https://github.com/double-break/spapi-php/issues/40 it has solution to fix the signature issue -

Also in order to satisfy amazon signature version 4 does not match error - I fixed it by creating a function for encoding marketplace id array square brackets using this function in client.php and replaced at line number 57 & 62 - public function custombuildquery($params, $encoding = PHP_QUERY_RFC3986) { if (!$params) { return ''; }

if ($encoding === false) { $encoder = function ($str) { return $str; }; } elseif ($encoding === PHP_QUERY_RFC3986) { $encoder = 'rawurlencode'; } elseif ($encoding === PHP_QUERY_RFC1738) { $encoder = 'urlencode'; } else { throw new \InvalidArgumentException('Invalid type'); }

$qs = ''; foreach ($params as $k => $v) { $k = $encoder($k); if (!is_array($v)) { $qs .= $k; if ($v !== null) { $qs .= '=' . $encoder($v); } $qs .= '&'; } else { foreach ($v as $aindex=>$vv) { $qs .= $k.rawurlencode('['.$aindex.']'); if ($vv !== null) { $qs .= '=' . $encoder($vv); } $qs .= '&'; } } }

return $qs ? (string) substr($qs, 0, -1) : ''; }