I am trying to get Azure AD user Attribute CompanyName using user id, passwor, client id, and Tenant Id using java.
Reffered this Doc: <https://learn.microsoft.com/en-us/graph/sdks/choose-authentication-providers?tabs=java> under username/password provider.
My Azure AD setup:
- in azure ad, ive created a user with company name property.
- Created Azure App registration, with localhost redirect URI & marked on id tokens under authetication section,
- created Client secret,
- Added role for App registration, added user to app regisitrion with that role under enterprise application.
- i've used client id, tenent id, user id, password correctly also tested these credentials with spring boot project there it is working fine (able to access comapnyName) after Login (SSO), but my requirement is i need to fetch user comapy details without SSO, for api use case.
here is my java code:
import java.util.Arrays;
import java.util.List;
import com.azure.identity.UsernamePasswordCredential;
import com.azure.identity.UsernamePasswordCredentialBuilder;
import com.microsoft.graph.authentication.TokenCredentialAuthProvider;
import com.microsoft.graph.models.User;
import com.microsoft.graph.requests.GraphServiceClient;
public class azureADUserAuth {
final String clientId = "YOUR_CLIENT_ID";
final String tenantId = "YOUR_TENANT_ID"; // or "common" for multi-tenant apps
final String userName = "YOUR_USER_NAME";
final String password = "YOUR_PASSWORD";
final List<String> scopes = Arrays.asList("User.Read");
public static void main (String[] args) {
try {
UsernamePasswordCredential credential = new UsernamePasswordCredentialBuilder()
.clientId(clientId).tenantId(tenantId).username(userName).password(password)
.build();
if (null == scopes || null == credential) {
throw new Exception("Unexpected error");
}
TokenCredentialAuthProvider authProvider = new TokenCredentialAuthProvider(scopes,credential);
GraphServiceClient graphClient = GraphServiceClient .builder() .authenticationProvider(authProvider) .buildClient();
User me = graphClient.me().buildRequest().get();
System.out.println(me.companyName);
}catch (Exception e) {
e.printStackTrace();
}
here code printing 5 errors in console
- com.microsoft.graph.core.ClientException: Error executing the request
- Caused by: java.io.IOException: java.util.concurrent.ExecutionException: com.azure.core.exception.ClientAuthenticationException: Failed to acquire token with username and password
- Caused by: java.util.concurrent.ExecutionException: com.azure.core.exception.ClientAuthenticationException: Failed to acquire token with username and password
- Caused by: com.azure.core.exception.ClientAuthenticationException: Failed to acquire token with username and password
- Caused by: com.microsoft.aad.msal4j.MsalServiceException: AADSTS7000218: The request body must contain the following parameter: 'client_assertion' or 'client_secret'. Trace ID: 8e0854e2-6d51-422e-b2d5-47b6303b2400 Correlation ID: 5df6d392-3bcb-4730-9666-4ae8f9131681 Timestamp: 2023-08-18 18:00:51Z
Please Help!