How to get timestamp and signature for Amazon Api

I am looking at reading Amazon Api. But I could not read the api as a URL and get the following message "Invalid timestamp or signature". How to get values ​​for both fields.

+4
source share
4 answers

Try following the instructions and formatting here: http://docs.aws.amazon.com/AWSECommerceService/latest/DG/rest-signature.html

The signature is particularly complex, as it must be calculated and encoded. "Calculate RFC 2104 compliant HMAC using the SHA256 hash algorithm using the line above with our" dummy "secret key: 1234567890. For more information about this step, see the documentation and code samples for your programming language."

The timestamp is usually in this format: UTC time 2009-01-01T12: 00: 00Z

I would try this api signed helper request: http://aws.amazon.com/code/Product-Advertising-API/2609

+3
source

Here I provide the code to sign the request.

$param = array();
$param['AWSAccessKeyId']   = USER_AWS_ACCESS_KEY_ID; 
$param['Action']           = 'GetFeedSubmissionList'; 
$param['SellerId']         = USER_MERCHANT_ID; 
$param['SignatureMethod']  = 'HmacSHA256'; 
$param['SignatureVersion'] = '2'; 
$param['Timestamp']        = gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time()); 
$param['Version']          = '2009-01-01'; 
$param['MarketplaceId']    = USER_MARKETPLACE_ID;  

$url = array();
foreach ($param as $key => $val){

 $key = str_replace("%7E", "~", rawurlencode($key));
 $val = str_replace("%7E", "~", rawurlencode($val));

 $url[] = "{$key}={$val}";
}

sort($url);

$arr   = implode('&', $url);

$sign  = 'GET' . "\n";
$sign .= str_replace("https://","",USER_AMAZON_URL) . "\n";
$sign .= '/' . "\n";
$sign .= $arr;

$signature = hash_hmac("sha256", $sign, USER_AWS_SECRET_ACCESS_KEY, true);
$signature = urlencode(base64_encode($signature));

$link  = USER_AMAZON_URL."?";
$link .= $arr . "&Signature=" . $signature;

$data = file_get_contents($link);
+1
source

, java:

public static String getSignature(String secret, String message) throws Exception {
    Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
    SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
    sha256_HMAC.init(secret_key);

    String hash = Base64.encodeBase64String(sha256_HMAC.doFinal(message.getBytes()));
    hash = URLEncoder.encode(hash, "UTF-8");

    return hash;
}

apache commons-codec

+1

,

0

Source: https://habr.com/ru/post/1531238/


All Articles