How to connect to SharePoint by using JAVA

Li, Forrest 0 Reputation points
2024-09-03T09:43:40.5433333+00:00

I used to be able to connect to SharePoint using a username and password in JAVA code, and then execute file uploads. But after the SharePoint update, I can no longer log in to SharePoint with my username and password in the code. My SharePoint URL is https://my.example.com before update, now my latest SharePoint URL is https://myexample.sharepoint.com. How can I connect to SharePoint in JAVA code to upload and modify files in the latest SharePoint?

Here is the code I used to connect to SharePoint before:

CredentialsProvider credentials = new BasicCredentialsProvider();
credentials.setCredentials(AuthScope.ANY, new NTCredentials("username", "password", "", "domain"));

HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credentials);

HttpHost target = new HttpHost("my.example1.com", 443, "https");


String responseBody = null;
HttpPost request = new HttpPost("/sites/pip/_api/contextinfo");

request.addHeader("Accept", "application/json;odata=verbose");
CloseableHttpResponse response = null;

try {
	response = httpclient.execute(target, request, context);
	responseBody = EntityUtils.toString(response.getEntity(), "UTF-8");
} finally {
	if (response != null)
		response.close();
}
Microsoft 365 and Office SharePoint Development
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.