参数Timestamp无效。原因:JavaScript中必须采用ISO8601格式

时间:2015-03-24 17:10:46

标签: javascript rest amazon

我试图调用亚马逊的RESTful API,但这似乎是一个糟糕的开始或什么的!

我收到了这条消息:

<ItemLookupErrorResponse xmlns="http://ecs.amazonaws.com/doc/2013-08-01/">
<Error>
<Code>InvalidParameterValue</Code>
<Message>
Value 2015-3-24T18:38:26Z for parameter Timestamp is invalid. Reason: Must be in ISO8601 format.
</Message>
</Error>
<RequestId>77d4ca11-4f95-42d2-b85a-a4042abded80</RequestId>
</ItemLookupErrorResponse>

这是JavaScript中的代码:

    var today = new Date();
    var day = today.getDate();
    var month = today.getMonth()+1;
    var year = today.getFullYear();
    var hours = today.getHours() < 10 ? "0"+today.getHours() : today.getHours(); 
    var seconds = today.getSeconds() < 10 ? "0"+today.getSeconds() : today.getSeconds();
    var mins = today.getMinutes() < 10 ? "0"+today.getMinutes() : today.getMinutes();
    var time = encodeURIComponent(year+"-"+month+"-"+day+"T"+hours+":"+mins+":"+seconds+"Z");
    var messageToEncrypt ="GET\nwebservices.amazon.com\n\
        /onca/xml\nAWSAccessKeyId=ACCESSID&ItemId=0679722769&&SignatureMethod=HmacSHA256&SignatureVersion=2&Operation=ItemLookup&ResponseGroup=ItemAttributes%2COffers%2CImages%2CReviews&Service=AWSECommerceService&Timestamp="+time+"&Version=2013-08-01";

    var sig = CryptoJS.HmacSHA256(messageToEncrypt, "SECRET KEY");

    var request = "http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=AccessID&Operation=ItemLookup&ItemId=0976925524&ResponseGroup=SalesRank&Version=2013-08-01&Timestamp="+time+"&Signature="+sig;


    function GET(url) {
        var oFrame = document.getElementById("MyAjaxFrame");
        if (!oFrame) {
            oFrame = document.createElement("iframe");
            oFrame.style.display = "none";
            oFrame.id = "MyAjaxFrame";
            document.body.appendChild(oFrame);
        }
        oFrame.src = url;
    }

    GET(request);

这里有什么不对吗? 感谢。

1 个答案:

答案 0 :(得分:2)

你可能在几个月和几天内错过了领先的零。您的值可能应为2015-03-24T18:38:26Z

与评论中建议的@Bergi一样 - 使用toISOString