Issue accesing APIM - listbyservice API through Java SDK

Test User 1 Reputation point
2022-12-12T09:01:38.28+00:00

Hi Experts,

I am new to the Azure APIs and trying to access the APIs from Azure API management service through an App registred in AAD. I have followed below doc to register the app and create secrets and given the app "Resource read" permission in the APIM.
Doc for app registration: https://learn.microsoft.com/en-us/azure/active-directory/develop/howto-create-service-principal-portal#assign-a-role-to-the-application

I ma trying to use the java sdk for the API access - https://github.com/Azure/azure-sdk-for-java/blob/azure-resourcemanager-apimanagement_1.0.0-beta.3/sdk/apimanagement/azure-resourcemanager-apimanagement/README.md

below is my code snippet:

public void getApis(){  
  
  logger.info("Env: AZURE_CLIENT_ID:{} AZURE_TENANT_ID:{}, AZURE_CLIENT_SECRET:{}, AZURE_SUBSCRIPTION_ID:{}", System.getenv("AZURE_CLIENT_ID"),  
                  System.getenv("AZURE_TENANT_ID"), System.getenv("AZURE_CLIENT_SECRET"), System.getenv("AZURE_SUBSCRIPTION_ID"));  
            
  AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE );  
  TokenCredential credential = new DefaultAzureCredentialBuilder()  
          .authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint())  
          .build();  
  
  ApiManagementManager manager = ApiManagementManager  
          .authenticate(credential, profile);  
  String resourceGroupName = "test-rg";  
  String serviceName = "chaitanya-test-apim-service";  
  
  PagedIterable<ApiContract> result =  manager.apis().listByService(resourceGroupName, serviceName);  
  List<String> apiList = new ArrayList<>();  
  
  for (ApiContract api: result) {  
      apiList.add(api.name());  
      System.out.println("API Name: " + api.name());  
  }  
}  

I have set all th required Environmental variables as described in github. When I run the code I get below message for the credential object - "DefaultAzureCredential authentication failed. ---> "

Java version: 11

Attaching screenshot for the app registration config and error FYR. Could you please help here in identifuying the issue?

269631-screenshot-2022-12-12-at-15008-pm.png269553-screenshot-2022-12-12-at-14825-pm.png

Microsoft Security | Microsoft Entra | Microsoft Entra ID
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.