I need to access Sharepoint through a Java app using Azure AD certificate authentication (https://learn.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azuread). The equivalent of this C# sample:
using Microsoft.SharePoint.Client;
using PnP.Framework;
using System;
using System.IO;
namespace AzureADCertAuth
{
class Program
{
static void Main(string[] args)
{
var authManager = new AuthenticationManager("00000000-0000-0000-0000-000000000000", "certpath.pfx", "password", "foo.onmicrosoft.com");
using (var cc = authManager.GetContext("https://foo.sharepoint.com/sites/sitename"))
{
cc.Load(cc.Web, p => p.Title);
cc.ExecuteQuery();
Console.WriteLine(cc.Web.Title);
Microsoft.SharePoint.Client.File file = cc.Web.GetFileByUrl("https://foo.sharepoint.com/sites/sitename/docpath.docx");
ClientResult<Stream> streamResult = file.OpenBinaryStream();
cc.ExecuteQuery();
using (StreamReader sr = new StreamReader(streamResult.Value))
{
string fileContents = sr.ReadToEnd();
}
}
}
}
}
This C# code is working fine for accessing Sharepoint, but I’m having trouble figuring out how to do it in Java.
I tried something like this
import com.azure.core.credential.AccessToken;
import com.azure.core.credential.TokenRequestContext;
import com.azure.core.http.HttpClient;
import com.azure.core.http.HttpMethod;
import com.azure.core.http.HttpRequest;
import com.azure.core.http.HttpResponse;
import com.azure.core.http.okhttp.OkHttpAsyncHttpClientBuilder;
import com.azure.identity.ClientCertificateCredential;
import com.azure.identity.ClientCertificateCredentialBuilder;
import java.net.MalformedURLException;
import java.net.URL;
public class SharepointTest {
public static void getAccessTokenCert() {
ClientCertificateCredential credential = new ClientCertificateCredentialBuilder().clientId(
"00000000-0000-0000-0000-000000000000")
.tenantId("11111111-1111-1111-1111-111111111111")
.pfxCertificate("certpath.pfx", "password")
.build();
AccessToken token = credential.getToken(new TokenRequestContext().addScopes("https://foo.sharepoint.com/.default")).block();
String url = "https://foo.sharepoint.com/sites/sitename/docpath.docx";
HttpClient httpClient = new OkHttpAsyncHttpClientBuilder().build();
HttpRequest request = null;
try {
request = new HttpRequest(HttpMethod.GET, new URL(url)).setHeader(
"Authorization", "Bearer " + token.getToken()).setHeader("Accept", "application/json;odata=verbose");
}
catch (MalformedURLException e) {
throw new RuntimeException(e);
}
HttpResponse response = httpClient.send(request).block();
String responseBody = response.getBodyAsString().block();
}
}
But requesting the token throws the following exception:
throwIfFatal detected a jvm fatal exception, which is thrown and logged below:: java.lang.NoClassDefFoundError: io/netty/handler/codec/DefaultHeaders$ValueValidator
Any ideas what I need to do differently?