Skip to content
Advertisement

List Azure AD using UsernamePasswordCredential provider

I am using UsernamePasswordCredential provider to connect to AAD and get de users using msgraph-sdk-java (https://github.com/microsoftgraph/msgraph-sdk-java), the code is the following:

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

        final TokenCredentialAuthProvider tokenCredentialAuthProvider = new TokenCredentialAuthProvider(Arrays.asList("https://graph.microsoft.com/.default"), usernamePasswordCredential);

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

        UserCollectionPage userCollectionpage = graphClient.users().buildRequest().get();

and I am receiving the following error:

java.io.IOException: java.util.concurrent.ExecutionException: com.azure.core.exception.ClientAuthenticationException: Failed to acquire token with username and password

any idea if I need configure somethings in azure active directory?

Advertisement

Answer

Your problem has been resolved through comments. Post it as an answer to end the thread:

  1. Make sure your account is not a personal account.

  2. Make sure you enable Allow public client flows.

enter image description here

  1. Using msal4j library and PublicClientApplication class to acquire a token with a username, password and scope.
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement