计算的签名与您提供的亚马逊签名不符

时间:2015-06-30 14:17:37

标签: amazon-web-services

"我们计算的请求签名与您提供的签名不匹配。检查您的AWS Secret Access Key和签名方法。有关详细信息,请参阅服务文档"

以下是我的SignatureHelper(基于Amazon类库)。

public string SignRequest(Dictionary<string, string> parametersUrl, Dictionary<string, string> parametersSignture)
{
    var secret = Encoding.UTF8.GetBytes(parametersSignture["Secret"]);
    var signer = new HMACSHA256(secret);

    var stringToSign = CalculateStringToSign(parametersUrl, parametersSignture);
    var toSign = Encoding.UTF8.GetBytes(stringToSign);

    var sigBytes = signer.ComputeHash(toSign);
    var signature = Convert.ToBase64String(sigBytes);

    return signature;
}

private static string CalculateStringToSign(IDictionary<string, string> parameters, IDictionary<string, string> parametersSignture)
{
    var sorted = new SortedDictionary<string, string>(parameters, StringComparer.Ordinal);

    var data = new StringBuilder();
    data.Append(parametersSignture["RequestMethod"]);
    data.Append("\n");

    var endpoint = new Uri(parametersSignture["EndPoint"]);

    data.Append(endpoint.Host);
    if (endpoint.Port != 443 && endpoint.Port != 80)
    {
        data.Append(":")
            .Append(endpoint.Port);
    }

    data.Append("\n");
    var uri = endpoint.AbsolutePath;
    if (uri.Length == 0)
    {
        uri = "/";
    }

    data.Append(UrlEncode(uri, true));
    data.Append("\n");

    foreach (var pair in sorted.Where(pair => pair.Value != null))
    {
        data.Append(UrlEncode(pair.Key, false));
        data.Append("=");
        data.Append(UrlEncode(pair.Value, false));
        data.Append("&");
    }

    var result = data.ToString();

    return result.Remove(result.Length - 1);
}

private static string UrlEncode(string data, bool path)
{
    var encoded = new StringBuilder();
    var unreservedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~" + (path ? "/" : string.Empty);

    foreach (char symbol in Encoding.UTF8.GetBytes(data))
    {
        if (unreservedChars.IndexOf(symbol) != -1)
        {
            encoded.Append(symbol);
        }
        else
        {
            encoded.Append("%" + string.Format("{0:X2}", (int)symbol));
        }
    }

    return encoded.ToString();
}

这是我的数据:

CalculateStringToSign POST

mws.amazonservices.com
/
AWSAccessKeyId=***&Action=SubmitFeed&FeedType=_POST_PRODUCT_DATA_&MWSAuthToken=****&Merchant=***&PurgeAndReplace=false&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp=2015-06-30T13%3A47%3A42Z&Version=2009-01-01

URL

"https://mws.amazonservices.com/?AWSAccessKeyId=***&Action=SubmitFeed&Merchant=***&MWSAuthToken=***&SignatureVersion=2&Timestamp=2015-06-30T13%3a47%3a42Z&Version=2009-01-01&Signature=bfSpx9m7PIH3CbKNkjLDwY9norfD8mbTROJyxYnCMSU%3d&SignatureMethod=HmacSHA256&FeedType=_POST_PRODUCT_DATA_&PurgeAndReplace=false+HTTP%2f1.1&x-amazon-user-agent=Developer(Language%3dc%23)"

这是我收到的回复

Code: SignatureDoesNotMatch
Message: The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.

我认为这是我帮助者的事情(不确定我在看多个代码示例时看起来是一样的。

谢谢,

克莱尔

1 个答案:

答案 0 :(得分:0)

@Michael - sqlbot是正确的。但是,在解决方案清理并重建之后,它可以工作: - )

对于任何苦苦挣扎的人,我建议使用https://mws.amazonservices.co.uk/scratchpad/index.html并复制'String to sign',如果你没有产生相同的响应,那么问题就在于你的代码,如果看到,你怎么编码它并将其附加到请求(我的问题)。