HARISH
HARISH

Reputation: 211

Microsoft Graph api Mail services with Spring Boot

I am using the Microsoft Graph api to send and get mails in my Spring Boot Application,

I had used the dependencies of graph api like

         <dependency>
             <groupId>com.microsoft.graph</groupId>
             <artifactId>microsoft-graph</artifactId>
             <version>3.5.0</version>
         </dependency>
         <dependency>
        <!-- This dependency is only needed if you are using the TokenCrendentialAuthProvider -->
        <groupId>com.azure</groupId>
        <artifactId>azure-identity</artifactId>
        <version>1.2.5</version>
    </dependency>

In code my imports are

import com.azure.identity.ClientSecretCredential;
import com.azure.identity.ClientSecretCredentialBuilder;
import com.microsoft.graph.authentication.TokenCredentialAuthProvider;
import com.microsoft.graph.models.BodyType;
import com.microsoft.graph.models.EmailAddress;
import com.microsoft.graph.models.ItemBody;
import com.microsoft.graph.models.Message;
import com.microsoft.graph.models.Recipient;
import com.microsoft.graph.models.UserSendMailParameterSet;
import com.microsoft.graph.requests.GraphServiceClient;
import com.microsoft.graph.requests.MessageCollectionPage;

Error coming in code at

ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder()
            .clientId(CLIENT_ID)
            .clientSecret(CLIENT_SECRET).tenantId(TENANT_GUID).build();
    
    TokenCredentialAuthProvider tokenCredentialAuthProvider = new TokenCredentialAuthProvider(SCOPES, clientSecretCredential);


    GraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider( tokenCredentialAuthProvider ).buildClient();
    

    MessageCollectionPage messagesPage = graphClient.me().messages()
            .buildRequest()
            .select("Sender,Subject")
            .top(10)
            .get();

I am getting a error like this in code.

 (DirectJDKLog.java:175) - Servlet.service() for servlet [dispatcherServlet] in context with path [/ldesk] threw exception [Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class com.azure.identity.implementation.IdentityClient] with root cause
java.lang.NoClassDefFoundError: Could not initialize class com.azure.identity.implementation.IdentityClient
    at com.azure.identity.implementation.IdentityClientBuilder.build(IdentityClientBuilder.java:113) ~[azure-identity-1.2.5.jar:?]
    at com.azure.identity.ClientSecretCredential.<init>(ClientSecretCredential.java:50) ~[azure-identity-1.2.5.jar:?]
    at com.azure.identity.ClientSecretCredentialBuilder.build(ClientSecretCredentialBuilder.java:62) ~[azure-identity-1.2.5.jar:?]

Upvotes: 1

Views: 9685

Answers (1)

unknown
unknown

Reputation: 7483

If using microsoft-graph 3.4.0, you cannot use microsoft-graph-auth to create an IAuthenticationProvider object.

preview:

ClientCredentialProvider authProvider = new ClientCredentialProvider(CLIENT_ID, SCOPES, CLIENT_SECRET, TENANT_GUID, NATIONAL_CLOUD);

IGraphServiceClient graphClient = GraphServiceClient
                .builder()
                .authenticationProvider(authProvider)
                .buildClient();

User user = graphClient.users(user_id).buildRequest().get();

New version:

First, add azure-identity in Pom.xml.

ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder()
        .clientId(clientId)
        .clientSecret(clientSecret)
        .tenantId(tenant)
        .build();

TokenCredentialAuthProvider tokenCredentialAuthProvider = new TokenCredentialAuthProvider(scopes, clientSecretCredential);

GraphServiceClient graphClient =
  GraphServiceClient
    .builder()
    .authenticationProvider(tokenCredentialAuthProvider)
    .buildClient();

User me = graphClient.users(user_id).buildRequest().get();

Upvotes: 6

Related Questions