JavaEar 专注于收集分享传播有价值的技术资料

签名错误 (signature error)

This shows error "The request signature we calculated does not match the signature you provided. Check your AWS.

     string url;
    integer statuscode;
    String date1=json.serialize(Datetime.now());
    if(date1.contains('"')){
    date1=date1.replace('"','');}
    String algorithmName = 'HmacSHA256';
    date1=date1.substring(0,(date1.length()-5));
     date1=date1+'Z';
     date1=EncodingUtil.UrlEncode(date1,'UTF-8');    
     String Action=EncodingUtil.UrlEncode('CreateTopic','UTF-8');
     String AccessKey=EncodingUtil.UrlEncode('APIKEY','UTF-8'); // API key hide due to security resion but i check it work fine for SES
     String Signaturemethod=EncodingUtil.UrlEncode('HmacSHA256','UTF-8');
     string str= 'GET\nsns.us-east-1.amazonaws.com\n/\nAction='+Action+'&Name=Testtopics&AWSAccessKeyId='+AccessKey+'&Timestamp='+date1+'&SignatureVersion=2&SignatureMethod='+Signaturemethod; 
    Blob mac = Crypto.generateMac(algorithmName,Blob.valueOf(str),Blob.valueOf('SECURITYKEY')); //blob value of key used in signature
    url='https://sns.us-east-1.amazonaws.com/?Action=CreateTopic&Name=Testtopics&AWSAccessKeyId=ACCESSKEY&Timestamp='+date1+'&SignatureVersion=2&SignatureMethod=HmacSHA256&Signature='+EncodingUtil.UrlEncode(EncodingUtil.base64Encode(mac),'UTF-8'); // final url   

2个回答

    最佳答案

  1. 英文原文

    I don't have much Idea about the AWS but below is what I was able to find. can't test these things anywhere to figue out the problem.

    A common cause of the error message below is not properly creating the string to sign, such as forgetting to URL encode characters such as the colon (:) and the forward slash (/) in Amazon S3 bucket names.

    <Error>
        <Type>Sender</Type>
        <Code>SignatureDoesNotMatch</Code>
        <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.</Message>
    </Error>
    

    http://docs.aws.amazon.com/general/latest/gr/signature-version-2.html

    http://docs.aws.amazon.com/AmazonS3/latest/dev/BucketRestrictions.html


    中文翻译

    我对AWS没有多少想法,但下面是我能找到的。无法在任何地方测试这些东西来解决问题。

      

    以下错误消息的常见原因是没有正确创建要签名的字符串,例如忘记URL编码字符,例如冒号(:)和Amazon S3存储桶名称中的正斜杠(/)。

     &LT;错误&GT;
        &LT;类型&GT;发件人&LT; /类型&GT;
        &LT;代码&GT; SignatureDoesNotMatch&LT; /代码&GT;
        &lt; Message&gt;我们计算的请求签名与您提供的签名不匹配。
        检查您的AWS Secret Access Key和签名方法。
        有关详细信息,请参阅服务文档。&lt; / Message&gt;
    &LT; /错误&GT;
     

    http://docs.aws.amazon。 COM /普通/最新/克/签名版本2.HTML

    http://docs.aws.amazon.com/AmazonS3/最新的/ dev / BucketRestrictions.html

    I don't have much Idea about the AWS but below is what I was able to find. can't test these things anywhere to figue out the problem.

    A common cause of the error message below is not properly creating the string to sign, such as forgetting to URL encode characters such as the colon (:) and the forward slash (/) in Amazon S3 bucket names.

    <Error>
        <Type>Sender</Type>
        <Code>SignatureDoesNotMatch</Code>
        <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.</Message>
    </Error>
    

    http://docs.aws.amazon.com/general/latest/gr/signature-version-2.html

    http://docs.aws.amazon.com/AmazonS3/latest/dev/BucketRestrictions.html

    我对AWS没有多少想法,但下面是我能找到的。无法在任何地方测试这些东西来解决问题。

      

    以下错误消息的常见原因是没有正确创建要签名的字符串,例如忘记URL编码字符,例如冒号(:)和Amazon S3存储桶名称中的正斜杠(/)。

     &LT;错误&GT;
        &LT;类型&GT;发件人&LT; /类型&GT;
        &LT;代码&GT; SignatureDoesNotMatch&LT; /代码&GT;
        &lt; Message&gt;我们计算的请求签名与您提供的签名不匹配。
        检查您的AWS Secret Access Key和签名方法。
        有关详细信息,请参阅服务文档。&lt; / Message&gt;
    &LT; /错误&GT;
     

    http://docs.aws.amazon。 COM /普通/最新/克/签名版本2.HTML

    http://docs.aws.amazon.com/AmazonS3/最新的/ dev / BucketRestrictions.html

  2. 参考答案2
  3. It looks like you might be missing a trailing single quote at the end of your url. See if that makes a difference.

    url="https://sns.us-east-1.amazonaws.com/?Action=CreateTopic&Name=Testtopics&AWSAccessKeyId=ACCESSKEY&Timestamp='+date1+'&SignatureVersion=2&SignatureMethod=HmacSHA256&Signature='+EncodingUtil.UrlEncode(EncodingUtil.base64Encode(mac),'UTF-8')+"'";