Get User details from Azure AD

Viewed 64

I have a user created in Azure Active Directory and i'm trying to read it's properties / attributes but i'm getting an error.

here is my code:

        UsernamePasswordProvider authProvider = new UsernamePasswordProvider(clientId, Collections.singletonList("https://graph.microsoft.com/.default"),
            username, password, NationalCloud.Global, tenantId, clientSecret);
        GraphServiceClient<Request> graphClient = GraphServiceClient
            .builder()
            .authenticationProvider(authProvider)
            .buildClient();

        User user = graphClient.me().buildRequest().get();
        System.out.println(user.companyName);

the error I get is :

SEVERE: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: com/microsoft/graph/httpcore/ICoreAuthenticationProvider] with root cause

these are the dependencies im using:

  <dependency>
        <groupId>com.microsoft.graph</groupId>
        <artifactId>microsoft-graph</artifactId>
        <version>5.34.0</version>
    </dependency>

    <dependency>
        <groupId>com.microsoft.graph</groupId>
        <artifactId>microsoft-graph-core</artifactId>
        <version>2.0.0</version>
    </dependency> 

    <dependency>
        <groupId>com.microsoft.graph</groupId>
        <artifactId>microsoft-graph-auth</artifactId>
        <version>0.3.0</version>
    </dependency>

Thank You.

1 Answers

Could you please check the docs to get the sample code for UsernamePasswordProvider - https://docs.microsoft.com/en-us/graph/sdks/choose-authentication-providers?tabs=Java#usernamepassword-provider .

final UsernamePasswordCredential usernamePasswordCredential = new UsernamePasswordCredentialBuilder() .clientId(clientId) .username(username) .password(password) .build();

final TokenCredentialAuthProvider tokenCredentialAuthProvider = new TokenCredentialAuthProvider(scopes, usernamePasswordCredential);

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

final User me = graphClient.me().buildRequest().get();

Hope this helps

Thanks

Related