12

Java application needs access to SharePoint 2013 REST API https://msdn.microsoft.com/en-us/library/office/jj860569.aspx

Would prefer to use BASIC authentication:

There are many examples of using the rest api's on the web but none seem to deal with authentication. Maybe I'm missing something really simple here.

This works manually via POSTMAN: http://tech.bool.se/basic-rest-request-sharepoint-using-postman/ but requires me to enter username and password in browser.

I've tried implementing this: HttpClientBuilder basic auth using

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

This results in -> WARNING: NTLM authentication error: Credentials cannot be used for NTLM authentication: org.apache.http.auth.UsernamePasswordCredentials

Community
  • 1
  • 1
Eric Nord
  • 4,674
  • 3
  • 28
  • 56

1 Answers1

10

Thanks @fateddy that does the trick: Remember to switch out UsernamePasswordCredentials("username", "password"));for NTCredentials(, , ,);

Using this maven dependency:

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

The authentication to SharePoint works:

import org.apache.http.client.CredentialsProvider;
import org.apache.http.auth.AuthScope;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.auth.NTCredentials;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;

public class SharePointClientAuthentication {

public static void main(String[] args) throws Exception {
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(
            new AuthScope(AuthScope.ANY),
            new NTCredentials("username", "password", "https://hostname", "domain"));
    CloseableHttpClient httpclient = HttpClients.custom()
            .setDefaultCredentialsProvider(credsProvider)
            .build();
    try {
        HttpGet httpget = new HttpGet("http://hostname/_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();
    }
}
}

And you end up with : HTTP/1.1 200 OK

Eric Nord
  • 4,674
  • 3
  • 28
  • 56
  • 1
    I tried your solution to authenticate my Java service with SP2013 Search endpoint but I still receive this error : "The SafeQueryPropertiesTemplateUrl \"The Sa feQueryPropertiesTemplateUrl "{0}" is not a valid URL.\" is not a valid URL." – Jonathan Anctil Dec 07 '15 at 20:11
  • Did you get it working? I re-ran the code and it should be good to go. Can anyone else verify? – Eric Nord Feb 02 '16 at 21:14
  • 1
    Yeah it finally works. I use WinHttpClients.createDefault(). But there was also some problems with the installation and I don't know all the things that the IT guys have made! – Jonathan Anctil Feb 02 '16 at 21:27
  • 1
    Thanks. I used your solution and it worked. But I had a problem logging in to the sites in sharepoint. And after sometime, I tried to inspect the IIS log files and found that the domain name I was using was wrong. Now all works well! – Dharshni Apr 28 '16 at 22:51
  • 4
    what is supposed to go in 'hostname' and 'domain' fields? – Franjo Pintarić Jul 26 '18 at 16:54
  • @FranjoPintarić hostname is the server you are authenticating against and domain is the Windows domain name. https://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/auth/NTCredentials.html – Eric Nord Jul 27 '18 at 16:44
  • 2
    I have to access sharepoint that is being authenticated by Company SAML. I have to connect to URL https://xyz.sharepoint.com/teams/x/y/z/AllItems.aspx and have to download file from this page. Could you please suggest how can i do this with Java RESTful client application? I have used above code to connect and am getting HTTP/1.1 403 Forbidden – Radhakrishna Feb 09 '19 at 07:14
  • @EricNord Have you tried to implement this kind of authentication but using the Microsoft Graph SDK? similar to this question [https://sharepoint.stackexchange.com/questions/261204/acess-sharepoint-using-microsoft-graph-sdk-for-java-without-an-access-token] – Selim Alawwa Apr 18 '19 at 12:23
  • @EricNord When connecting in your method I get HTTP/1.1 500 Internal Server Error, Do you know if its an issue in server or code? – Selim Alawwa Apr 21 '19 at 10:03
  • Getting a HTTP/1.1 403 Forbidden with the above solution. – Saideep Ullal May 10 '22 at 11:00