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?