gpt4 book ai didi

java - 无法使用 adal4j Api 获取 Azure token

转载 作者:太空宇宙 更新时间:2023-11-04 10:11:18 27 4
gpt4 key购买 nike

我正在尝试从 Microsoft 图形读取用户配置文件/图像,并使用 adal4j-1.5.0.jar 生成 azure token ,以便基于 token 我可以调用图形 API/Microsoft delve。

我在下面的代码中遇到问题。在下面的行之后移动到finally block 很简单,不会生成 token 或任何异常。“ 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();
}

最佳答案

adal4j-1.5.0的一些依赖似乎没有下载,请检查您项目中的jar包文件。根据我的测试,如果我使用 adal4j 1.5.0。我发现项目中缺少 adal4j-1.5.0 的一些依赖项。然后我无法获取访问 token 。

enter image description here

但是如果我使用 adal4j 1.0.0,它对我来说可以正常工作。如果版本 1.0.0 可以接受,您可以使用它作为解决方法或手动添加依赖项。

enter image description here

测试演示代码:

 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);
}

关于java - 无法使用 adal4j Api 获取 Azure token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52270540/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com