0

I am working on salesforce integration with Amazon using Amazon MWS. I am getting error code 403 and status : Forbidden. "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."

I have used every possible way to resolve this but getting same error again and again. Code is given below:

public class Amazon_Integration {

public String action = 'SubmitFeed';
public String FeedType ='_POST_PRODUCT_DATA_';
Public string accessKey = 'AKIexampleOKHWA';
Public string MarketplaceId = 'AexampleDER';
Public string SellerId = 'AUexampleRCX';
Public string SignatureMethod='HmacSHA256';
Public string SignatureVersion='2';    
Public string version='2009-01-01';
Public string amazonSecretKey='9YexampleWQMeM';
Public string mwstoken = 'amzn.mws.example.9f650';
public string endpoint = 'https://mws.amazonservices.com/Feeds/2009-01-01';        

//Prepare the feed 
public String prepare_body(String sellerId){

   String xmlBody = '<?xml version="1.0" encoding="UTF-8"?>' + 
                 '<AmazonEnvelope xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">'+
                     '<Header>' +
                     '<DocumentVersion>1.01</DocumentVersion>' +
                     '<MerchantIdentifier>' + sellerId + '</MerchantIdentifier>' +
                     '</Header>' +
                     '<MessageType>Product</MessageType>' +                         
                     '<Message>' +
                         '<MessageID>1</MessageID>' +
                         '<OperationType>Update</OperationType>' +
                         '<Product>' +
                             '<SKU>56789</SKU>' +
                             '<StandardProductID>' +
                             '<Type>ASIN</Type>' +
                             '<Value>B0EXAMPLEG</Value>' +
                             '</StandardProductID>' +
                             '<ProductTaxCode>A_GEN_NOTAX</ProductTaxCode>' +
                             '<DescriptionData>' +
                                 '<Title>Example Product Title</Title>' +
                                 '<Brand>Example Product Brand</Brand>' +
                                 '<Description>This is an example product description.</Description>' +
                                 '<BulletPoint>Example Bullet Point 1</BulletPoint>' +
                                 '<BulletPoint>Example Bullet Point 2</BulletPoint>' +
                                 '<MSRP currency="USD">25.19</MSRP>' +
                                 '<Manufacturer>Example Product Manufacturer</Manufacturer>' +
                                 '<ItemType>example-item-type</ItemType>' +
                              '</DescriptionData>' +
                              '<ProductData>' +
                                '<Health>' +
                                  '<ProductType>' +
                                    '<HealthMisc>' +
                                      '<Ingredients>Example Ingredients</Ingredients>' +
                                      '<Directions>Example Directions</Directions>' +
                                    '</HealthMisc>' +
                                  '</ProductType>' +
                                 '</Health>' +
                               '</ProductData>' +
                            '</Product>' +
                         '</Message>' +
                       '</AmazonEnvelope>';                                              

        return xmlBody;
}

public String get_contentmd5(String xmlBody){
        Blob targetBlob = Blob.valueOf(xmlBody);
        Blob hash = Crypto.generateDigest('MD5', targetBlob);    
        String contentMD5 = EncodingUtil.base64Encode(hash);
        contentMD5 =  EncodingUtil.URLENCODE(contentMD5,'UTF-8');                    
        return contentMD5;
}

public string send_feed_to_amazon(String xmlBody, String ContentMD5){

    String timestamp = datetime.now().formatGMT('yyyy-MM-dd\'T\'HH:mm:ss.SSS\'Z\'');
    timestamp = EncodingUtil.urlEncode(timestamp,'UTF-8');

    //Construct a query string with the query information
    String queryString = 'AWSAccessKeyId=' + accessKey + 
        '&Action=SubmitFeed'+
        '&ContentMD5Value=' + ContentMD5 +
        '&FeedType=_POST_PRODUCT_DATA_'+
        '&MWSAuthToken=' + mwstoken +   
        '&MarketplaceId.Id.1=' + MarketplaceId  +
        '&SellerId=' + SellerId +                                         
        '&SignatureMethod=' + SignatureMethod  +
        '&SignatureVersion=' + SignatureVersion  +
        '&Timestamp=' + timestamp  +
        '&Version=' + version;

    String stringtoSign = 'POST' + '\n' +
        'mws.amazonservices.com' + '\n' +
        '/Feeds/2009-01-01' + '\n' +
        queryString;               

    //Covert query string to signature using Amazon secret key as key
    Blob mac = Crypto.generateMac('HMacSHA256', 
    blob.valueof(stringtoSign),blob.valueof(amazonSecretKey));

    String signature = EncodingUtil.base64Encode(mac);
    signature = EncodingUtil.urlEncode(signature,'UTF-8');        

   //Create a new request
   HttpRequest request = new HttpRequest();    

   request.setEndpoint(endpoint +'?AWSAccessKeyId=' + accessKey +
                       '&Action=SubmitFeed'+                                                                                 
                       '&FeedType=_POST_PRODUCT_DATA_'+                                                      
                       '&MWSAuthToken=' + mwstoken +
                       '&MarketplaceIdList.Id.1=' + marketplaceId +
                       '&SellerId=' + sellerId +
                       '&ContentMD5Value=' + ContentMD5 +
                       '&SignatureMethod='+ signatureMethod +
                       '&SignatureVersion=2' +
                       '&Timestamp=' + timestamp +
                       '&Version=' + version +                           
                       '&Signature=' + signature);

    request.setMethod('POST');
    request.setHeader('Content-type', 'text/xml');        
    request.setBody(xmlBody);        

    Http http = new Http();
    try {
        HttpResponse response = http.send(request);
        string status=response.getStatus();           
        System.debug(response.getStatus());
        System.debug(response.getStatusCode());            
        System.debug(response.getBody());            
        return status;
    } 
    catch(System.CalloutException e) {
        System.debug(e);
        return string.valueOf(e);
    }        
}

Please help!!

Thanks in advance!!

1
  • did you tried running this on any other tool? there might be the case where some the items which you are passing in your API might be encoded and you are passing it decoded. Feb 14 '18 at 10:59
0

I have fixed this issue. I used Amazon Scratchpad and made StringToSign and endpoint URL just like mantioned in scratchpad. Now it is working. If anyone has the issue regarding this, can email me : abhishek.singh3107@gmail.com.

The key to make run these MWS apis is, just follow the MWS scratchpad.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.