Search code examples
javarestsharepoint-2010

SharePoint 2010 REST Java authentication


I was happy to access SharePoint using PowerShell. It just picked -DefaultCredential and I didn't have to worry about that. That was for prototyping.

But my actual code is Java. Now I am not sure about this at all.

Even though I make REST calls, even SOAP would fail if I don't authenticate properly.

Method 1 : NTLM

Here the only thing I am not sure about is the workstation ID. I login using Citrix to a VM and there is an explicit Workstation ID. I use that. Returns 401.

        DefaultHttpClient client = new DefaultHttpClient();

        HttpGet request = new HttpGet("http://teams.host.com/_vti_bin/listdata.svc/");

        NTCredentials credentials = new NTCredentials("user", 'pass', "workstation", "Domain");

        client.getCredentialsProvider().setCredentials(new AuthScope("teams.host.com",80), credentials);

        HttpResponse response = client.execute(request);

Method 2 : Basic authentication.

        HttpGet request = new HttpGet("http://teams.host.com/_vti_bin/listdata.svc/");

        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();

        credentialsProvider.setCredentials(AuthScope.ANY, 
                                           new UsernamePasswordCredentials("user", "password"));
        CloseableHttpClient httpClient = 
                    HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).build();


        HttpResponse response = httpClient.execute(request);

Returns 401.

What other method do I use ? Digest ? Since I don't know how -DefaultCredential in PowerShell worked I am back to the drawing board.

How should I investigate this ? I must be making some basic mistakes in this Java code. The flow is not right. That is my supposition.


Solution

  • So from Apache HttpClient this is the code that connects to SharePoint 2010. The workstation ID is the one used when I use Citrix XenDesktop to login to a Windows machine. I am able to get the result of my REST Get request.

    This uses NTLM authentication.

        DefaultHttpClient client = new DefaultHttpClient();
    
        HttpGet request = new HttpGet("http://teams.host.com/_vti_bin/listdata.svc/");
    
        NTCredentials credentials = new NTCredentials("user", 'pass', "workstation", "Domain");
    
        client.getCredentialsProvider().setCredentials(new AuthScope("teams.host.com",80), credentials);
    
        HttpResponse response = client.execute(request);