• KARAN SINGH 102
  • NEWBIE
  • -1 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
I am trying to connect to amazon MWS and fetch some report but ever time the API produces the same error  
<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>

THE CODE
Datetime now = Datetime.now().addDays(1);
    //String timestamp = now.formatGmt('yyyy-MM-dd')+'T'+now.formatGmt('HH:mm:ss')+'.'+now.formatGMT('SS')+'Z';
    String timestamp = now.formatGmt('yyyy-MM-dd')+'T'+now.formatGmt('HH:mm:ss')+'Z';
    DateTime start_dt = DateTime.newInstance(2014, 12, 16,15,21,30);
    DateTime end_dt = DateTime.newInstance(2014, 12, 18,14,17,21);
    //String start_date = start_dt.formatGmt('yyyy-MM-dd')+'T'+start_dt.formatGmt('HH:mm:ss')+'.'+start_dt.formatGMT('SS')+'Z';
    //String end_date = end_dt.formatGmt('yyyy-MM-dd')+'T'+end_dt.formatGmt('HH:mm:ss')+'.'+end_dt.formatGMT('SS')+'Z';
    String start_date = start_dt.formatGmt('yyyy-MM-dd')+'T'+start_dt.formatGmt('HH:mm:ss')+'Z';
    String end_date = end_dt.formatGmt('yyyy-MM-dd')+'T'+end_dt.formatGmt('HH:mm:ss')+'Z';

String request = 'AWSAccessKeyId='+urlencode('-***MY CODE*****-')+
            '&Action='+urlencode('RequestReport') +
            '&ReportType=' + urlencode( '_GET_MERCHANT_LISTINGS_DATA_')  +
            //'&StartDate=' + urlencode(start_date) +
            //'&EndDate=' + urlencode(end_date)+
            '&MWSAuthToken='+urlencode('-**** MY TOKEN******-')+
            //'&Marketplace='+urlencode('***ID***') +
            '&MarketplaceIdList.Id.1='+urlencode('-****ID*****-')+
            '&Merchant='+urlencode('-****ID*****-') +
            '&SignatureMethod='+urlencode('HmacSHA1') +
            '&SignatureVersion='+urlencode('2')+
            '&Timestamp=' + urlencode(timestamp) +                                                                            
            '&Version=' + urlencode('2009-01-01');


String canonical =  'GET'+'\n'+
                      'mws.amazonservices.ca\n'+
                      '/\n'+
                      request;
    String key = '-****KEY****-';
    
    //Signature HmacSHA256
    
    //Blob bsig = Crypto.generateMac('HMacSHA1', Blob.valueOf(canonical), Blob.valueOf(key));
    //Blob  b =Crypto.generateDigest('MD5',bsig );
    //Blob bsig =Crypto.generateDigest('HmacSHA256', Blob.valueOf(canonical), Blob.valueOf(key));
     //Blob bsig = Crypto.encryptWithManagedIV('AES256',  Blob.valueOf(key),Blob.valueOf(canonical));
    
    //String signature = EncodingUtil.base64encode(bsig);
    
        String c = EncodingUtil.base64Encode(Blob.valueOf(canonical));
        String signature ;
        String signingKey = EncodingUtil.base64Encode(Blob.valueOf(key));
        Blob mac = Crypto.generateMac('HMacSHA1', blob.valueof(c),blob.valueof(signingKey)); 
        signature = EncodingUtil.base64Encode(mac);                
        //return macUrl;
  
  
    System.debug('String to sign===>\n' + canonical );
    System.debug('Signature===>\n' + signature );

String body =request+'&Signature=' + urlEncode(signature);
   
   
   System.debug('canonical ===>\n' + canonical);
    System.debug('Body===>\n' + body );
   
   
        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();
        req.setCompressed(true);
        req.setHeader('Content-Type','text/xml');
         req.setHeader('User-Agent','Salesforce');
        req.setEndpoint('https://mws.amazonservices.ca/?'+body);
        req.setMethod('GET');
        //req.setBody(body);
           
           /*req.setHeader('Host','salesforce.com');
           req.setHeader('x-amazon-user-agent','cloud/1.0');*/
      
        res = http.send(req); 
  
   
   system.debug(res.getbody());
   
   
    return null;
    
    
    
    
    }

     private String urlEncode(String rawValue) {
        String value = (rawValue == null) ? '' : rawValue;
        String encoded = null;

        try {
            encoded = EncodingUtil.urlEncode(rawValue, 'UTF-8') 
                .replace('+', '%20')
                .replace('*', '%2A')
                .replace('%7E','~');
        } catch (Exception e) {
            //System.err.println("Unknown encoding: " + CHARACTER_ENCODING);
            //e.printStackTrace();
        }

        return encoded;
    }

I have done every possible altaration on Signing algo,Url encoding as there docs but all were invain...