Skip to content

List Azure AD using UsernamePasswordCredential provider

I am using UsernamePasswordCredential provider to connect to AAD and get de users using msgraph-sdk-java (, the code is the following:

            final UsernamePasswordCredential usernamePasswordCredential = new UsernamePasswordCredentialBuilder()

        final TokenCredentialAuthProvider tokenCredentialAuthProvider = new TokenCredentialAuthProvider(Arrays.asList(""), usernamePasswordCredential);

        final GraphServiceClient graphClient =

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

and I am receiving the following error: java.util.concurrent.ExecutionException: Failed to acquire token with username and password

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


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.