javagradlemicrosoft-graph-apionedrive

Microsoft Graph Java SDK ClassNotFoundException


Here is my build.gradle

dependencies {
    implementation 'com.azure:azure-identity:1.7.1'
    implementation 'com.microsoft.graph:microsoft-graph:5.42.0'
}

Here is my code (actual values are replaced with "...")

final ClientSecretCredential credential = new ClientSecretCredentialBuilder()
    .clientId("...")
    .clientSecret("...")
    .tenantId("...")
    .build();
final TokenCredentialAuthProvider authProvider = new TokenCredentialAuthProvider(
    List.of("files.readwrite.all"),
    credential
);
final GraphServiceClient client = GraphServiceClient
    .builder()
    .authenticationProvider(authProvider)
    .buildClient();

And once I run it I get a java.lang.ClassNotFoundException: com.azure.core.credential.TokenCredential

I tried to find a solution to this, but nothing seems to help.


Solution

  • Following code works:

    import java.util.Arrays;
    import java.util.List;
    
    import com.azure.identity.ClientSecretCredential;
    import com.azure.identity.ClientSecretCredentialBuilder;
    import com.microsoft.graph.authentication.TokenCredentialAuthProvider;
    import com.microsoft.graph.models.User;
    import com.microsoft.graph.requests.GraphServiceClient;
    import okhttp3.Request;
    
            final String clientId = "your client id";
            final String tenantId = "your tenant id";
            final String clientSecret = "your client secret";
    
            final List<String> scopes = Arrays.asList("https://graph.microsoft.com/.default");
    
            final ClientSecretCredential credential = new ClientSecretCredentialBuilder()
                .clientId(clientId).tenantId(tenantId).clientSecret(clientSecret).build();
    
            if (null == scopes || null == credential) {
                throw new Exception("Unexpected error");
            }
            final TokenCredentialAuthProvider authProvider = new TokenCredentialAuthProvider(
                scopes, credential);
    
            final GraphServiceClient<Request> graphClient = GraphServiceClient.builder()
                .authenticationProvider(authProvider).buildClient();
    

    My pom entries:

    <dependency>
        <groupId>com.microsoft.graph</groupId>
        <artifactId>microsoft-graph</artifactId>
        <version>5.54.0</version>
    </dependency>
    <dependency>
        <groupId>com.azure</groupId>
        <artifactId>azure-identity</artifactId>
        <version>1.7.1</version>
    </dependency>
    <dependency>
        <groupId>com.azure</groupId>
        <artifactId>azure-core</artifactId>
        <version>1.35.0</version>
    </dependency>
    <dependency>
        <groupId>com.microsoft.graph</groupId>
        <artifactId>microsoft-graph-auth</artifactId>
        <version>0.3.0</version>
    </dependency>
    <dependency>
        <groupId>com.microsoft.graph</groupId>
        <artifactId>microsoft-graph-core</artifactId>
        <version>2.0.18</version>
    </dependency>
    <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>okhttp</artifactId>
        <version>4.10.0</version>
    </dependency>