0

I'm struggling to authenticate with MS SharePoint to use it's API. I've been googling and playing around with that problem for a while but somehow I can't figure out a solution. The most promising solution so far is based on this answer.

The dependencies I'm using are:

    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.4.1</version>
    </dependency>

This is my code:

public static void callRestEasyService() throws Exception {
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(
            new AuthScope(AuthScope.ANY),
            new NTCredentials("user", "password", "https://workstation.de", "domain.de"));

    CloseableHttpClient httpclient = HttpClients.custom()
            .setDefaultCredentialsProvider(credsProvider)
            .build();

    try {
        HttpGet httpget = new HttpGet("https://adress/_api/web/lists");

        System.out.println("Executing request " + httpget.getRequestLine());
        CloseableHttpResponse response = httpclient.execute(httpget);
        try {
            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            EntityUtils.consume(response.getEntity());
        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }

}

The code is quite self-explanatory I think, it is basically just what the linked answer suggests. My investigation showed that the best way to go for this problem are the NTCredentials. I also tried some other alternatives like the Basic Authentication but in all cases I receive:

HTTP/1.1 401 Unauthorized

I also tried using Samba JCIFS as an alternative NTLM engine.

Furthermore I'm a little bit scared that maybe the parameter for workstation (3rd parameter) or domain is filled in incorrectly by me. The documentation says:

workstation - The workstation the authentication request is originating from. Essentially, the computer name for this machine.

So I tried filling in the name of my machine but many examples on the web suggest that you use an URL that you are trying to authenticate with. This caused a little confusion for me but with none of the two options I could get it working.

Does anyone know why that is or has a possible solution or a workaround for that problem? Is it maybe possible that the SharePoint restricts the access via a programmed client? As far as I know it's at least not possible to disable the API from the SharePoint. Any ideas / thoughts to this?

LOLWTFasdasd asdad
  • 2,625
  • 5
  • 26
  • 39

1 Answers1

0

I did not manage to get it going with the apache http client instead of this I tried running a cURL request from my java code to approach the SharePoint API, this worked fine instead. In case any one else has a similiar problem and wants to try a workaround this is my source code to run the cURL request:

ProcessBuilder pb = new ProcessBuilder(
            "curl",
            "https://mysharepoint/_api/web/lists/getbytitle('listName')/items",
            "-v",
            "--ntlm",
            "--negotiate",
            "-u",
            "user:password"
    );

    Process p = pb.start();
    InputStream is = p.getInputStream();
    return createHashMapForStaff(convertStreamToString(is));

The return line (createHashMapForStaff(convertStreamToString(is)) is just adapting the retrieved .XML from the SharePoint to my needs. The InputStream from the Process p is basically what you need.

LOLWTFasdasd asdad
  • 2,625
  • 5
  • 26
  • 39