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:
Make sure your account is not a personal account.
Make sure you enable
Allow public client flows
.
- Using msal4j library and PublicClientApplication class to acquire a token with a username, password and scope.