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
AKumAKum 

Amazon / Salesforce Feed api error ?

Hi Guys , 

By following some other posts I tried to submit a sample feed but its returning error "Invalid query string provided - Keys may not contain &lt" . I think its not related to signature but not sure what I am missing .

public with sharing class SendFeedToAmazon {
public static void ListProduct() {
        //Current time in GMT ISO 8601
        String timestamp = datetime.now().formatGMT('yyyy-MM-dd\'T\'HH:mm:ss.SSS\'Z\'');
        timestamp = EncodingUtil.urlEncode(timestamp,'UTF-8');
        
        //Amazon Variables
        String action = 'SubmitFeed';
        String FeedType ='_POST_PRODUCT_DATA_';
        String version = '2009-01-01';
        String signatureVersion = '2';
        String signatureMethod = 'HmacSHA256';
        String marketplaceId= 'Example';
        String sellerId = 'Example';
        String endpoint = 'https://mws.amazonservices.com/Feeds/2009-01-01';
        String accessKey = 'Example';
        String amazonSecretKey = 'Example';
    
        //Construct a query string with the query information
        String queryString = 'AWSAccessKeyId=' + accessKey + 
            '&Action=' + action  +
            '&FeedType=' + FeedType + 
            '&MarketplaceIdList.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');

        System.debug(signature);
     
        HttpRequest req = new HttpRequest(); 
        
        //Below is the MD5Value
        
        String xmlBody = '<?xml version="1.0" encoding="iso-8859-1"?>' +
'<AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'+
    'xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">'+
  '<Header>'+
    '<DocumentVersion>1.01</DocumentVersion>'+
    '<MerchantIdentifier>M_EXAMPLE_123456</MerchantIdentifier>'+
  '</Header>'+
  '<MessageType>Product</MessageType>'+
  '<PurgeAndReplace>false</PurgeAndReplace>'+
  '<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>';
        
        
        
        Blob targetBlob = Blob.valueOf(xmlBody);
        Blob hash = Crypto.generateDigest('MD5', targetBlob);    
        String contentMD5 = EncodingUtil.base64Encode(hash);
        contentMD5 =  EncodingUtil.URLENCODE(contentMD5,'UTF-8');   
        
        req.setEndpoint(endpoint +'?AWSAccessKeyId=' + accessKey +
            '&Action=' + action +
            '&FeedType=' + FeedType  +
            '&ContentMD5Value=' + ContentMD5+
            '&SellerId=' + sellerId +
            '&SignatureVersion=2' +
            '&Timestamp=' + timestamp +
            '&Version=' + version +
            '&Signature=' + signature +
            '&SignatureMethod='+ signatureMethod +
            '&MarketplaceIdList.Id.1=' + marketplaceId);
        
        req.setMethod('POST');
        req.setBody(xmlBody);
        
        
        Http http = new Http();
        try {
            HttpResponse res = http.send(req);
            System.debug(':::::' + res.getStatus());
            System.debug(':::::' + res.getStatusCode());
            System.debug(':::::' + res.getBody());
            } 
        catch(System.CalloutException e) {
            System.debug(e);
        }
	}
}