1

I was trying to callout Share point Service from apex using REST API.Which is having windows authentication which is of type NTLM. But i was getting 401 Unauthorized access.But i was able to access form SOAPUI and from browser.

Http objhttp;
HttpRequest req ;
HttpResponse res ;
objhttp = new Http();
req = new HttpRequest();
res = new HttpResponse();  
req.setMethod('GET');
req.setEndpoint('my end point url');
Blob headerValue = Blob.valueOf('domain"\"username'+':'+'password');
String authorizationHeader = 'NTLM '+EncodingUtil.base64Encode(headerValue);
system.debug('*** authorizationHeader '+authorizationHeader );
// Set the necessary Headers
req.setHeader('Authorization',authorizationHeader);
req.setHeader('Accept','application/vnd.cpc.authreturn+xml');
req.setHeader('Content-type','application/vnd.cpc.authreturn+xml; charset=UTF-8');
req.setHeader('Accept-Language', 'en-CA');
req.setHeader('Content-Length', '200');
req.setTimeout(120000);
system.debug('*** req is '+req);
try {
res = objhttp.send(req);
System.debug('res values in req: '+ res);
response=res.getBody();
system.debug('*** response is '+response);
}
catch(System.CalloutException e) {
System.debug('Callout error : '+ e);
 }

This was the code I am using to do call outs.Previously when the authentication is 'basic' i am getting proper response, but now when it changed to windows authentication by using 'NTLM' its giving 401 unauthorized error. But SOAPUI is giving proper response using 'NTLM'. Is there any possibility to call Sharepoint having windows authentication using this NTLM ..

  • Have you figured out how to call an NTLM web service? I'm getting 415 error. – Andrii Muzychuk Sep 1 '16 at 18:22
  • @AndrewMuza no i couldn't figured how to call NTML services, we have changed the authentication mechanism on endpoint services – rajasekhar Sep 3 '16 at 6:16
  • 1
    I figured it out. I missed the thing with headers in request. I put header into request first. Then I saw discussion about 415 error. Additional parameter was added to send method of the library. After I passed map of header key/value to the method it started working like a charm. – Andrii Muzychuk Sep 4 '16 at 8:12
  • @andriimuzychuk - I am facing the same error. Can you please have a look at this - salesforce.stackexchange.com/questions/219404/… – Samir May 25 '18 at 10:09
2

It should first send you a challenge. Does it not even do that?

Have you checked this NTLM implementation in Apex? https://github.com/natewallace/ApexNTLM

  • Thanks for the link Willem, its not sending any challenge , just giving 401 unauthorized access... but when i access with SOAPUI giving proper result.. – rajasekhar Sep 17 '15 at 9:06
  • Are you 100% certain the request body and headers are identical from Apex and SOAPUI? Could you post them here? – Willem Mulder Sep 17 '15 at 10:23
  • 1
    That make me really confusing ... i feel the headers are not matching . In SOAP UI i am not passing any headers, just in authentication section i am giving uname,password and domain name. You can see my salesforce headers above. But the diff i observed is SOAP UI encoded authorization code is not matching with salesforce ones in salesforce i am giving as domain/username : password but SOAP UI is taking as uname password and domain. – rajasekhar Sep 17 '15 at 10:38
  • I suggest mark this as an answer with one addition - github.com/natewallace/ApexNTLM/issues/3 – Andrii Muzychuk Sep 4 '16 at 8:14

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.