2

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>();
authpref.add(AuthPolicy.NTLM);
// 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();
System.out.println(EntityUtils.toString(entity));

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

2
  • 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
    Commented Mar 30, 2015 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. Commented Mar 30, 2015 at 9:15

2 Answers 2

1

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.PasswordAuthentication;
import java.net.URL;

public class NtlmTest01 {

    public static void main(String[] args) throws Exception {

        String urlStr = “http://example.com/root/action.dll?p1=value1″;
        String domain = “”; // May also be referred as realm
        String userName = “CHANGE_ME”;
        String password = “CHANGE_ME”;      

        String responseText = getAuthenticatedResponse(urlStr, domain, userName, password);

        System.out.println(”response: ” + responseText);
    }

    private static String getAuthenticatedResponse(final String urlStr, final String domain, final String userName, final String password) throws IOException {

        StringBuilder response = new StringBuilder();

        Authenticator.setDefault(new Authenticator() {
            @Override
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(domain + “\\” + userName, password.toCharArray());
            }
        });

        URL urlRequest = new URL(urlStr);
        HttpURLConnection conn = (HttpURLConnection) urlRequest.openConnection();
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestMethod(”GET”);

        InputStream stream = conn.getInputStream();
        BufferedReader in = new BufferedReader(new InputStreamReader(stream));
        String str = “”;
        while ((str = in.readLine()) != null) {
            response.append(str);
        }
        in.close();     

        return response.toString();
    }

}

Notest: NTLM Handshack ————————————————

GET 401 Unauthorized WWW-Authenticate: NTLM ————————————————

GET Authorization: NTLM TlRMTVNTUAABAAAAA7IAAAoACgApAAAACQAJACAAAABMSUdIVENJVFlVUlNBLU1JTk9S

401 Unauthorized WWW-Authenticate: NTLM TlRMTVNTUAACAAAAAAAAACgAAAABggAAU3J2Tm9uY2UAAAAAAAAAAA== ————————————————

GET Authorization: NTLM TlRMTVNTUAADAAAAGAAYAHIAAAAYABgAigAAABQAFABAAAAADAAMAFQAAAASABIAYAAAAAAAAACiAAAAAYIAAFUAUgBTAEEALQBNAEkATgBPAFIAWgBhAHAAaABvAGQATABJAEcASABUAEMASQBUAFkArYfKbe/jRoW5xDxHeoxC1gBmfWiS5+iX4OAN4xBKG/IFPwfH3agtPEia6YnhsADT

200 Ok

————————————————

Ref: http://www.innovation.ch/personal/ronald/ntlm.html

0

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();
    context.setCredentialsProvider(credsProvider);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.