0
votes

I am trying to call the Azure API Management Rest Api with Adal 4 Java and based on following example: https://blogs.msdn.microsoft.com/azureossds/2015/06/23/authenticating-azure-resource-management-rest-api-requests-using-java/

The code works fine for the part where I take the pregenerated Access-Token from the API Management UI, but I fail when I try to do it programatically.

Working code:

String accessToken = "sometoken";
HttpGet request = new HttpGet("https://testapics.management.azure-api.net/apis/subscription/?api-version=2017-03-01");
request.addHeader("Authorization", "Bearer " + accessToken);
response = client.execute(request);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

What are the Identifier, Primary and Secondary key needed for in relation to the example. Do I still have to register the rest api as an application in Azure AD and create a service principal for it?

This is the failing part:

private AuthenticationResult getAccessTokenFromUserCredentials() throws Exception {
    AuthenticationContext context = null;
    AuthenticationResult result = null;
    ExecutorService service = null;
    try {
        service = Executors.newFixedThreadPool(1);
        context = new AuthenticationContext(AUTHORITY, false, service);

        ClientCredential credential = new ClientCredential("integration", "passwort");
        Future<AuthenticationResult> future = context.acquireToken("https://management.azure.com/", credential, null);
        result = future.get();
    } finally {
        service.shutdown();
    }
    if (result == null) {
        throw new ServiceUnavailableException("authentication result was null");
    }
    return result;
}

I always get com.microsoft.aad.adal4j.AuthenticationException: {"error_description":"AADSTS70001: Application with identifier 'integration' was not found in the directory when trying to execute with token generation.

Thanks

2

2 Answers

0
votes

The error message is telling you that you need to put the client id of the app that you have registered in Azure AD here:

ClientCredential credential = new ClientCredential("client-id-guid-here", "client-secret-goes-here");

You also need to put the client secret of the afore-mentioned app there.

0
votes

I figured out the problem myself. There is an example on how to create the accessToken yourself via MAC SHA512 Hash for C# and I successfully ported it to java. So there is no need to call the Authorization Server when staying with the admin user.