I am trying to create an authentication method to autheticate to azure ad for adding an removal of users via my java code. I have a free tier account setup for the same. below is the method I used for authentication:
try {
final AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE);
final TokenCredential credential = new DefaultAzureCredentialBuilder()
.authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint())
.build();
AzureResourceManager azureResourceManager = AzureResourceManager
.configure()
.withLogLevel(HttpLogDetailLevel.BASIC)
.authenticate(credential, profile)
.withDefaultSubscription();
runSample(azureResourceManager, profile);
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
public static boolean runSample(AzureResourceManager azureResourceManager, AzureProfile profile) {
final String userEmail = Utils.randomResourceName(azureResourceManager, "test", 15);
final String userName = userEmail.replace("test", "Test ");
final String spName = Utils.randomResourceName(azureResourceManager, "sp", 15);
final String raName1 = Utils.randomUuid(azureResourceManager);
final String raName2 = Utils.randomUuid(azureResourceManager);
final String groupEmail1 = Utils.randomResourceName(azureResourceManager, "group1", 15);
final String groupEmail2 = Utils.randomResourceName(azureResourceManager, "group2", 15);
final String groupName1 = groupEmail1.replace("group1", "Group ");
final String groupName2 = groupEmail2.replace("group2", "Group ");
String spId = "";
try {
// ============================================================
// Create a user
System.out.println("Creating an AD user " + userName + "...");
//
ActiveDirectoryUser user = azureResourceManager.accessManagement().activeDirectoryUsers()
.define(userName)
.withEmailAlias(userEmail)
.withPassword(Utils.password())
.create();
//
System.out.println("Created AD user " + userName);
Utils.print(user);
} catch (Exception e) {
System.out.println("Did not create Service Principal in Azure. No clean up is necessary");
}
}
these details in the environment. Where client secret I extracted after registering an application in the portal. I am not able to complete the authentication by the above logic. Can anyone please help me here ?





runSamplecode) and 2) The error you are getting.