0

我有这个字符串,我需要在其中使用 JavaScript 重新构造。

Label=11121212&TopicArn=test&AWSAccountId.member.1=YYYYYYY&ActionName.member.1=createTopic&Action=AddPermission&Version=2010-03-31& AWSAccessKeyId =XXXXXXXXX&SignatureVersion=2&SignatureMethod=HmacSHA1&Timestamp=2012-05-02T16%3A06%3A09.000Z&Signature=C

对于此示例,AWSAccessKeyId应该是字符串的第一部分,并且label应该是倒数第二部分。还有其他类似的。

预期输出--AWSAccessKeyId = XXXXXXXXX&AWSAccountId.member.1 = YYYYYYYYY&动作= AddPermission&ActionName.member.1 =发布&标签= IOS-SNS-权限标签&签名= dEaNL0ibP5c7xyl4qXDPFPADW0meoUX9caKyUIx1wkk%3D&是SignatureMethod = HmacSHA256&SignatureVersion = 2&时间戳= 2012-05-02T00%3A51%3A23.965Z&TopicArn = arn%3Aaws%3Asns%3Aus-east-1%3A335750469596%3AiOSGoesWooooo-1335919882&Version=2010-03-31

创建此错误字符串的代码

exports.generatePayload = function(params, accessKeyId, secretKey, endpoint) {
    var host = endpoint.replace(/.*:\/\//, "");
    var payload = null;

    var signer = new AWSV2Signer(accessKeyId, secretKey);
    params = signer.sign(params, new Date(), {
        "verb" : "POST",
        "host" : host,
        "uriPath" : "/"
    });

    var encodedParams = [];
    for(var key in params) {
        if(params[key] !== null) {
            encodedParams.push(encodeURIComponent(key) + "=" + encodeURIComponent(params[key]));
        } else {
            encodedParams.push(encodeURIComponent(key));
        }
    }
    payload = encodedParams.join("&");
    return payload;
}

我尝试将它放在一个数组中并对其进行重组,但它对我不起作用。

请建议如何使用 JavaScript 轻松完成此操作

4

1 回答 1

1
exports.generatePayload = function(params, accessKeyId, secretKey, endpoint) {
    var host = endpoint.replace(/.*:\/\//, "");
    var payload = null;

    var signer = new AWSV2Signer(accessKeyId, secretKey);
    params = signer.sign(params, new Date(), {
        "verb" : "POST",
        "host" : host,
        "uriPath" : "/"
    });

    var encodedParams = [];
    if(params["AWSAccessKeyId"] != null)
    {
        encodedParams.push(encodeURIComponent("AWSAccessKeyId") + "=" + encodeURIComponent(params["AWSAccessKeyId"]));

    }
    if(params["AWSAccountId.member.1"] != null)
    {
        encodedParams.push(encodeURIComponent("AWSAccountId.member.1") + "=" + encodeURIComponent(params["AWSAccountId.member.1"]));

    }
    //other_ifs_for_param_keys_here
    payload = encodedParams.join("&");
    return payload;
于 2012-05-02T16:53:26.873 回答