SQS客户端验证异常 - 我们计算的请求签名与您提供的签名不匹配

时间:2017-06-20 07:55:24

标签: c# .net amazon-web-services amazon-s3

我尝试验证AWS S3队列:使用以下参数: 访问密钥,密钥,队列URL

public bool ValidateSqs(string queue_url, string access_key, string secret_key)
{

            if (string.IsNullOrWhiteSpace(access_key)
               || string.IsNullOrWhiteSpace(secret_key)
               || string.IsNullOrWhiteSpace(queue_url))
                return false;

            try
            {
                SqsClient sqsValid = new SqsClient(access_key, secret_key, queue_url);
                return sqsValid.CheckAwsCredentials();
            }
            catch (Exception ex)
            {
                LogUtils.Error("fail to validate SQS", ex);
                return false;
            }
        }

这是SqsClient:

public class SqsClient
{
    private AmazonSQSClient _awsSQSClient;
    private readonly int MaxRetryAttempts = 3;

    public SqsClient(string accessKey, string secretKey, string url)
    {
        if (string.IsNullOrEmpty(accessKey))
        {
            throw new ArgumentNullException("accessKey");
        }

        if (string.IsNullOrEmpty(secretKey))
        {
            throw new ArgumentNullException("secretKey");
        }

        if (string.IsNullOrEmpty(url))
        {
            throw new ArgumentNullException("url");
        }

        AccessKey = accessKey;
        SecretKey = secretKey;
        Url = url;

        try
        {
            RestartClient();
        }
        catch (Exception ex)
        {
            LogUtils.WithParameters()
                .Append(x => AccessKey)
                .Append(x => SecretKey)
                .Error("Failed to create SQS Client", ex);

            throw;
        }
    }

 public bool CheckAwsCredentials()
        {
            try
            {
               _awsSQSClient.GetQueueAttributes(new GetQueueAttributesRequest
                {
                    QueueUrl = Url,
                    AttributeNames = new List<string> { "All" }
                });

                return true;
            }
            catch (Exception ex)
            {
                LogUtils.Error(ex);
                return false;
            }
        }

从CheckAwsCredentials()方法抛出异常。

  

Amazon.SQS.AmazonSQSException:我们计算的请求签名   与您提供的签名不符。检查您的AWS Secret   访问密钥和签名方法。请参阅服务文档   的信息。

     

此请求的规范字符串应该是&#39; POST   / 49512474474 / DEXQueue

     

内容类型:application / X WWW的窗体-urlencoded;字符集= utf-8的   主持人:sqs.ap-southeast-2.amazonaws.com   user-agent:aws-sdk-dotnet-45 / 3.3.2.4 aws-sdk-dotnet-core / 3.3.8.1   .NET_Runtime / 4.0 .NET_Framework / 4.0   OS / Microsoft_Windows_NT_10.0.14393.0 ClientSync   X-AMZ-内容SHA256:92f6fc97d2a609de283521acc0f05e5fabe54f4afe7427217ef2fc5521   的x AMZ-日期:20170620T072209Z

     

的内容类型;主机;用户代理; X-AMZ-内容-SHA256; X-AMZ-日期   92f6fc97d2a609d5e283521acc0bbccf05e5fabe54f4afe7427217ef2fc5521&#39;

错误的原因是什么?

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

发生这种情况的原因有很多,包括:

  • 密钥复制/粘贴错误
  • Secret Key mangling 错误(字符串转义出错)
  • 不小心将 QueueURL 用作 ServiceURL