I am making rest api calls to Sharepoint 2013 using Java. How can I connect to the sharepoint 2013 using jersey rest client?
Note: currently I am using apache http components and NTCredentials
class
Credentials credentials=new NTCredentials(username, password, workstation, domain);
AuthScope authScope=new AuthScope(AuthScope.ANY);
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(authScope,credentials);
CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
How to adopt this to Jersey framework?
The following code executes an NTLM authenticated HTTP GET request that using Jersey:
public Response executeRestGet(String user, String pass) {
Client client = ClientBuilder.newClient(prepareClientConfig(user, pass));
WebTarget target = client.target("http://localhost/").path("site/_api/xxxxx");
return target.request(HTTP_ACCEPT_JSON).get();
}
private ClientConfig prepareClientConfig(String user, String pass) {
ClientConfig clientConfig = new ClientConfig();
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
//make sure to supply all 4 arguments to the NTCredentials constructor
credentialsProvider.setCredentials(AuthScope.ANY, new NTCredentials(user, pass, null, null));
clientConfig.property(ApacheClientProperties.CREDENTIALS_PROVIDER, credentialsProvider);
clientConfig.connectorProvider(new ApacheConnectorProvider());
return clientConfig;
}
Please note that this approach requires: jersey-apache-connector
. Maven Dependency:
<dependency>
<groupId>org.glassfish.jersey.connectors</groupId>
<artifactId>jersey-apache-connector</artifactId>
<version>2.22.2</version>
</dependency>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With