签名错误

时间:2013-10-10 07:37:15

标签: string encoding utf-8 apex-code amazon-sns

这显示错误“我们计算的请求签名与您提供的签名不匹配。请检查您的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 个答案:

答案 0 :(得分:0)

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

  

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

<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

答案 1 :(得分:0)

您的网址末尾可能会遗漏一个尾随的单引号。看看是否有所作为。

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')+"'";