如何获取Amazon Api的时间戳和签名

时间:2014-03-11 20:05:40

标签: api amazon

我正在阅读亚马逊Api。但是无法将api作为URL读取并收到以下消息"无效的时间戳或签名"。如何获取这两个字段的值。

3 个答案:

答案 0 :(得分:4)

请尝试按照说明进行操作: http://docs.aws.amazon.com/AWSECommerceService/latest/DG/rest-signature.html

签名特别困难,因为必须对其进行计算和编码。 "使用上面的字符串和我们的" dummy"使用SHA256哈希算法计算符合RFC 2104的HMAC。秘密访问密钥:1234567890。有关此步骤的更多信息,请参阅编程语言的文档和代码示例。"

时间戳通常采用以下格式:UTC时间2009-01-01T12:00:00Z

我会尝试这个api signed requests helper:http://aws.amazon.com/code/Product-Advertising-API/2609

答案 1 :(得分:1)

我在这里提供签署请求的代码。

$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);

答案 2 :(得分:1)

这可能有点晚了,但我刚刚使用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