• Marie-Charlotte D
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
Hello,

I need to connect to an external site to send objects. I use the code below but I still get an error ("wsse authentication" required) . 
Does somebody see the error?

Thank you for your help!
 
        String jsonLeads = JSON.serialize(leadToSend);        
        String headerReq = createHeader();
        
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setHeader('Authorization',headerReq);
        system.debug('header '+request.getHeader('Authorization'));
        request.setEndpoint('http://xxxx');
        request.setMethod('POST');
        request.setBody(jsonLeads);
        system.debug('request '+request);
        
        try{
            HttpResponse response = http.send(request);
            if (response.getStatusCode() != 201) {
                System.debug('The status code returned was not expected: ' +
                             response.getStatusCode() + ' ' + response.getStatus()+response.getBody());
            } else {
                System.debug(response.getBody());
            }
        }catch(System.CalloutException e) {
            system.debug(e);
        }       
    }
    
    public static String createHeader(){
        String token = 'xxxxxx';
        String chars = '0123456789abcdef';
        String nonce = '';
        String dt = system.now()+'Z';
        
        while (nonce.length() < 32) {
            Integer idx = Math.mod(Math.abs(Crypto.getRandomInteger()), chars.length());
            nonce += chars.substring(idx, idx+1);
        }
        system.debug('nonce: '+nonce);
        String nonce64 = EncodingUtil.base64Encode(Blob.valueOf(nonce));
        
        Blob target = Blob.valueOf(nonce+dt+token); 
        Blob digestSha1 = Crypto.generateDigest('SHA1',target);
        String digest = EncodingUtil.base64Encode(digestSha1);
        
        String header = 'X-WSSE:UsernameToken Token="'+token+'",PasswordDigest="'+digest+'",Nonce="'+nonce64+'",Created="'+dt+'"';       
        return header;
    }