We are trying to connect to Sharepoint 2013 using rest based api using a java program. Our code is -

DefaultHttpClient httpclient = new DefaultHttpClient();
List<String> authpref = new ArrayList<String>();
// httpclient.getParams().setParameter(AuthPNames.CREDENTIAL_CHARSET,
// authpref);
NTCredentials creds = new NTCredentials(userName, password, "portal", "xyz.com");
httpclient.getCredentialsProvider().setCredentials(AuthScope.ANY, creds);

HttpHost target = new HttpHost("portal.xyz.com", 80);

// Make sure the same context is used to execute logically related
// requests
HttpContext localContext = new BasicHttpContext();

// Execute a cheap method first. This will trigger NTLM authentication
HttpGet httpget = new HttpGet("http://portal.xyz.com/Sites/XYZteam/_api/web/GetFolderByServerRelativeUrl('/Sites/XYZteam/GlobalBrandBook')/Files");
httpget.addHeader("Accept", "application/json;odata=verbose");
httpget.addHeader("X-HTTP-Method", "GET");
httpget.addHeader(HttpHeaders.USER_AGENT, "Mozilla/4.0+(compatible;+MSIE+5.01;+Windows+NT+5.0");
HttpResponse response = httpclient.execute(target, httpget, localContext);
HttpEntity entity = response.getEntity();

We also tried basic authentication/ Invoking SOAP Based Services but every time we keep getting 403 Forbidden error. Any clues will be of great help. Tina

  • 3
    We understand you're having issues, but posting essentially the same question 3 times does not help. Instead, pretty please, stick with the first question. 1st: sharepoint.stackexchange.com/questions/136457/…, 2nd (already closed as duplicate): sharepoint.stackexchange.com/questions/136461/access-token-java – Jussi Palo Mar 30 '15 at 6:41
  • 1
    Thanks for your help but I am no way related to the person who has posted that question. And I didnt know there was a question posted like that. Secondly the solution you proposed dont work for me. I still get 403 error. Is there anything that we need to do on sharepoint side which can help me in fixing this error. – Tina Agrawal Mar 30 '15 at 9:15

Try this, it works for me

    httpClient = HttpClients.createDefault();
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY, new NTCredentials(username, password, "", ""));
    HttpHost target = new HttpHost(host, 80, "http");

    HttpClientContext context = HttpClientContext.create();

Thanks. Tried it already but didnt work. What worked for me is this - http://www.muneebahmad.com/index.php/archives/127

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.