首页 > 解决方案 > Not able to get Azure token using adal4j Api

问题描述

am trying to read user profile/image form Microsoft graphic and am using adal4j-1.5.0.jar to generate the azure token so that based on token i can make a call to graphic API/Microsoft delve.

i am facing issue in below code. it was simple moving to finally block after below line without generating token or any exception. "Future future = context.acquireToken(resourceUri, credential, null);"

String clientId = "clientid";
String clientSecret = "cleintsecret";
String resourceUri = "https://graph.microsoft.com/v1.0/me";

String redirectUri = "http://localhost:9082/contextroot";

String authorityUri ="https://login.microsoftonline.com/{tenent id}/oauth2/authorize";


AuthenticationContext context = null;
AuthenticationResult result = null;
ExecutorService service = null;
try {
    service = Executors.newFixedThreadPool(1);
    context = new AuthenticationContext(authorityUri, false, service);
    ClientCredential credential = new ClientCredential(clientId,clientSecret);

    Future<AuthenticationResult> future = context.acquireToken(resourceUri, credential, null);



    result = future.get();
}
finally {
    service.shutdown();
}

标签: javaspringazureadaladal4j

解决方案


It seems that some dependencies of adal4j-1.5.0 are not download as well, please check the jar package files from your project. Based on my test if I use the adal4j 1.5.0. I found that are some dependencies of adal4j-1.5.0 are missing from the project. Then I can't get the access token.

enter image description here

But If I use the adal4j 1.0.0, it works correctly for me. If the version 1.0.0 is acceptable, you could use that as a workaround or add the dependencies manually.

enter image description here

Test demo code:

 private static final String APP_ID = "clientId";
 private static final String APP_SECRET = "secret key";
 private static final String TENATID = "xxxxx";
 public static void main(String[] args) throws Exception {
 String authority = "https://login.microsoftonline.com/"+TENATID; 
 String resourceUrl = "https://graph.microsoft.com"; //Microsoft graph. AD graph: https://graph.windows.net
 ExecutorService service = Executors.newFixedThreadPool(1);
 AuthenticationContext context = new AuthenticationContext(authority, true, service);
        // Acquire Token
 Future<AuthenticationResult> result = context.acquireToken(
                resourceUrl,
                new ClientCredential(APP_ID, APP_SECRET),
                null
        );
        String token = result.get().getAccessToken();
        System.out.println(token);
    }

推荐阅读