php - Signature does not match in Amazon Web Services -
i writing php code amazon web services. code.
<?php function amazonencode($text) { $encodedtext = ""; $j = strlen($text); ($i = 0; $i < $j; $i++) { $c = substr($text, $i, 1); if (!preg_match("/[a-za-z0-9-_.~]/", $c)) { $encodedtext .= sprintf("%%%02x", ord($c)); } else { $encodedtext .= $c; } } return $encodedtext; } function amazonsign($url, $secretaccesskey) { // 0. append timestamp parameter $url .= "×tamp=" . gmdate("y-m-dth:i:sz"); // 1a. sort utf-8 query string components parameter name $urlparts = parse_url($url); parse_str($urlparts["query"], $queryvars); ksort($queryvars); // 1b. url encode parameter name , values $encodedvars = array(); foreach ($queryvars $key => $value) { $encodedvars[amazonencode($key)] = amazonencode($value); } // 1c. 1d. reconstruct encoded query $encodedqueryvars = array(); foreach ($encodedvars $key => $value) { $encodedqueryvars[] = $key . "=" . $value; } $encodedquery = implode("&", $encodedqueryvars); // 2. create string sign $stringtosign = "get"; $stringtosign .= "n" . strtolower($urlparts["host"]); $stringtosign .= "n" . $urlparts["path"]; $stringtosign .= "n" . $encodedquery; // 3. calculate rfc 2104-compliant hmac string created, // secret access key key, , sha256 hash algorithm. if (function_exists("hash_hmac")) { $hmac = hash_hmac("sha256", $stringtosign, $secretaccesskey, true); } elseif (function_exists("mhash")) { $hmac = mhash(mhash_sha256, $stringtosign, $secretaccesskey); } else { die("no hash function available!"); } // 4. convert resulting value base64 $hmacbase64 = base64_encode($hmac); // 5. use resulting value value of signature request parameter // (url encoded per step 1b) $url .= "&signature=" . amazonencode($hmacbase64); echo $url; } $url = 'http://webservices.amazon.com/onca/xml?service=awsecommerceservice&awsaccesskeyid=something&associatetag=something&operation=itemsearch&keywords=mustang&searchindex=blended&condition=collectible×tamp=2016-08-08t12%3a00%3a00z&version=2013-08-01'; $secret_key = 'my_secret_key'; $url = amazonsign($url, $secret_key); ?>
this code returns me url. use url inside browser can search results using url gives me error.
signaturedoesnotmatchthe request signature calculated not match signature provided. check aws secret access key , signing method. consult service documentation details.
i using these awsaccesskeyid
, secret_key
.
it's $stringtosign .= "n"
should $stringtosign .= "\n"
might not problem. if use official php sdk amazon instead of relying on custom scripts you'll have less issues.
Comments
Post a Comment