function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
harsh_chandraharsh_chandra 

SignatureDoesNotMatch

Hi All,

I am facing a issue when trying to create a signedURL for amazon private file in salesforce.

<Error><Code>SignatureDoesNotMatch</Code>

<Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.

</Message>

 

Is there any method for creating 'Time Limited Signed URL'. Please let me know

 

 

Here is my code for creating signature:


        Datetime now = system.now();
        String formattednow = now.formatGmt('yyyy-MM-dd')+'T'+now.formatGmt('HH:mm:ss')+'.'+now.formatGMT('SSS')+'Z';

       
        String canonical = 'AmazonS3'+'ListAllMyBuckets'+formattednow; //"AmazonS3" + OPERATION + Timestamp       
        
        Blob bsig = Crypto.generateMac('HmacSHA1', Blob.valueOf(canonical), Blob.valueOf(secret));       
        
        String signature = EncodingUtil.base64Encode(bsig);

 

Thanks in advance

 

Best Answer chosen by Admin (Salesforce Developers) 
harsh_chandraharsh_chandra

After few more hours of research, I found that:

Amazon web services have a class named 'AWS_S3_ExampleController' which tells the exact way to create a signature. You just need to provide bucketname and filename to call the method i.e redirectToS3Key.

 

Thanks,

Harsh Chandra

All Answers

Anup JadhavAnup Jadhav

I am afraid the question does not contain enough information for us to provide a concrete answer. Can you elaborate what "amazon private file in salesforce" means?

 

You might want to check out the documentation on Amazon's website and see what's missing when you create the signature.

 

- Anup

harsh_chandraharsh_chandra

After few more hours of research, I found that:

Amazon web services have a class named 'AWS_S3_ExampleController' which tells the exact way to create a signature. You just need to provide bucketname and filename to call the method i.e redirectToS3Key.

 

Thanks,

Harsh Chandra

This was selected as the best answer
Sunay - KVP Bus SolnsSunay - KVP Bus Solns

Hi Harsh, Even I faced the same problem when I was trying to make an request to amazon to get the order Report, but still I get the same error Signature does not match. Please see the code below:

 

DateTime d = Datetime.now();
String timestamp = d.formatGmt('yyyy-MM-dd')+'T'+ d.formatGmt('HH:mm:ss')+'.'+ d.formatGMT('SSS')+'Z';
System.debug(timestamp);
String urlEncodedTimestamp = EncodingUtil.urlEncode(timestamp, 'UTF-8');
String action = 'ListOrders';
String inputStr = action + timestamp;
String algorithmName = 'HMacSHA256';
Blob mac = Crypto.generateMac(algorithmName, Blob.valueOf(inputStr),Blob.valueOf('AWS Secret Key'));
System.debug(mac);
String macUrl = EncodingUtil.urlEncode(EncodingUtil.base64Encode(mac), 'UTF-8');
System.debug(macUrl);

 

 

Please help me if I am doing any mistake in the code to generate a signature. 

 

 

harsh_chandraharsh_chandra

Hi,

Basically my requirement is to create a time based url which should expire after certain time interval. For that I need to create signature. 

However, you can refer the following code for creating signature just provide your bucket name and filename :

String filename = EncodingUtil.urlEncode('Your File Name Name', 'UTF-8');
Datetime now = DateTime.now();
Datetime expireson = now.AddSeconds(120);
Long Lexpires = expireson.getTime()/1000;
String stringtosign = 'GET\n\n\n'+Lexpires+'\n/'+'Your Bucket Name'+'/'+'Your File Name Name';
String signed = make_sig(stringtosign);

system.debug('----------my signature = '+signed);


private String make_sig(string canonicalBuffer)

{
String macUrl ;
String signingKey = EncodingUtil.base64Encode(Blob.valueOf(as3.secret));
Blob mac = Crypto.generateMac('HMacSHA1', blob.valueof(canonicalBuffer),blob.valueof(as3.secret));
macUrl = EncodingUtil.base64Encode(mac);
return macUrl;
}

 

Thanks 

Sunay - KVP Bus SolnsSunay - KVP Bus Solns

Hi,

 

I have written the code as per Amazon Developer guide to generate the signature. Also I am using Amazon MWS scratchpad to compare the "Stringtosign" with my application 'Stringtosign" and everything seems to be in place.

 

Unfortunately still it says the "Signaturedoesnotmatch", I am not sure where the error is coming from.

 

Can you help me please if there is anything I need to check/correct, as Amazon support is also not able to help me on the same and they are taking a longer time to respond.

 

 

harsh_chandraharsh_chandra

Can you please send me the code that you are using to generate signature?

Sunay - KVP Bus SolnsSunay - KVP Bus Solns

Hi,

 

Please find the code below and do the needful.

 

public class ListOrders{

// This class will send a request to Amazon to get the Order Date and Order Number

 

public static void Listorder() {
// The date format is yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
DateTime d = datetime.now();
String timestamp = d.format('yyyy-MM-dd')+'T'+ d.format('HH:mm:ss')+'.'+ d.format('SSS')+'Z';
System.debug(timestamp);
String urlEncodedTimestamp = EncodingUtil.urlEncode(timestamp, 'UTF-8');
String action = 'ListOrders';
//String inputStr = action + timestamp;
//String algorithmName = 'HMacSHA256';
//Blob mac = Crypto.generateMac(algorithmName, Blob.valueOf(inputStr),Blob.valueOf('ieoEXAMPLEElk+fNGEXAMPLElRtPowPUozVujT'));
//System.debug(mac);
//String macUrl = EncodingUtil.urlEncode(EncodingUtil.base64Encode(mac), 'UTF-8');
//System.debug(macUrl);

String urlToTest = 'amazon.com';
String version = '2011-01-01';
String SignatureVersion = '2';
String SignatureMethod = 'HmacSHA256';
String MarketplaceId= 'ATEXAMPLE0DER';
String FulfillmentChannel = 'MFN';
String SellerId = 'A1EXAMPLE5UUUI';
String endpoint = 'https://mws.amazonservices.com/Orders/2011-01-01/';
String accessKey = 'AKIAJEXAMPLEWYSFDLMA';

//String querystring = '?AWSAccessKeyId=' + accessKey +
// '&Action=' + action +
// '&MarketplaceId.Id.1=' + MarketplaceId +
// '&FulfillmentChannel.Channel.1=' + FulfillmentChannel +
// '&SellerId=' + SellerId +
// '&SignatureVersion='+SignatureVersion +
// '&SignatureMethod='+ SignatureMethod +
// '&Timestamp=' + urlEncodedTimestamp +
// '&Version=' + version;



//String StringtoSign = 'GET\n\n\n'+endpoint+'\n/'+querystring;

String inputstr = 'AWSAccessKeyId=' + accessKey + '\n' +
'&Action=' + action + '\n' +
'&MarketplaceId.Id.1=' + MarketplaceId + '\n' +
'&SellerId=' + SellerId + '\n'+
'&SignatureMethod=' + SignatureMethod + '\n' +
'&SignatureVersion=' + SignatureVersion + '\n' +
'&Timestamp=' + timestamp + '\n' +
'&Version=' + version ;

String StringtoSign = 'POST'+ '\n' +
'mws.amazonservices.com'+'\n'+
'/Orders/2011-01-01' + '\n' +
inputstr ;


System.debug(StringtoSign);

String signed = make_sig(StringtoSign);

System.debug(signed);

String codedsigned = EncodingUtil.urlEncode(signed,'UTF-8');

System.debug(codedsigned);

HttpRequest req = new HttpRequest();
req.setEndpoint(endpoint +'?AWSAccessKeyId=' + accessKey +
'&Action=' + action +
'&MarketplaceId.Id.1=' + MarketplaceId +
'&SellerId=' + SellerId +
'&Signature=' + codedsigned +
'&SignatureVersion='+SignatureVersion +
'&SignatureMethod='+ SignatureMethod +
'&Timestamp=' + urlEncodedTimestamp +
'&Version=' + version );
req.setMethod('POST');
Http http = new Http();
try {
HttpResponse res = http.send(req);
System.debug('STATUS:'+res.getStatus());
System.debug('STATUS_CODE:'+res.getStatusCode());
System.debug('BODY: '+res.getBody());
} catch(System.CalloutException e) {
System.debug('ERROR: '+ e);
}
}

public static String make_sig(String stringvalue)
{
String macUrl ;
String signingKey = EncodingUtil.base64Encode(Blob.valueOf('ieoEXAMPLEElk+fNGEXAMPLElRtPowPUozVujT'));
Blob mac = Crypto.generateMac('HMacSHA1', blob.valueof(stringvalue),blob.valueof(signingKey));
macUrl = EncodingUtil.base64Encode(mac);
return macUrl;
}

}

Sunay - KVP Bus SolnsSunay - KVP Bus Solns

Hi Harsh,

 

I was able to solve the issue and got the response. But when I try the same with other class its giving me the same error. I checked for all the possibilites but was not able to find out the issue.

 

When I hardcode the request got from Amazon MWS scratchpad, it is executing successfully but not when I give the values dynamically.

 

You have any idea why this error might be. If so can you please help me on the same.

 

MKHalfordMKHalford

Hi Sunay

 

I am looking for a VB.NET example of getting data from out Amazon MWS account. The code you have supplied, do you have a VB.NET version of it or can you advise on any place where I might get some VB examples. I have been looking around for a while but have been unable to find any good working examples.

 

Cheers

 

Mike

 

Sunay - KVP Bus SolnsSunay - KVP Bus Solns

Hi Mike,

 

I am sorry I do not have an idea about VB.NET because this is a Java code which I was working on. I guess you can use same style of coding in >net with the right syntax, which might help you.

 

 

MKHalfordMKHalford
Thanks anyway. I will continue my search. Mike Halford Technical Director Gozuku Ltd. Email: mike@gozuku.com Tel: 07932980113