java - 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();
}
解决方案
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.
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.
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);
}
推荐阅读
- amazon-iam - AWS EKS:无法将 IAM 角色附加到 pod
- firebase - 有没有办法让谷歌云功能知道它在 Firebase 模拟器中运行?
- python - LazyLoadImages 和 Beautifulsoup 网页抓取
- devexpress - GridLookUpEdit 从数据库加载数据
- python - 高程图分析
- mysql - 如何允许在 MacOS 上的 MySQL Docker 容器中使用更大的数据库大小
- r - 试图在R中的数据框中按组获取频率计数和百分比
- javascript - 未捕获的 ReferenceError:未定义要求。从 NodeJs 替换到网站后
- django - 这是一个难点:允许用户在 django 上以可扩展的方式通过自己的 gmail 发送电子邮件
- vba - 复制表和创建链接的功能失败并出现错误 3011