【问题标题】:Not able to get Azure token using adal4j Api无法使用 adal4j Api 获取 Azure 令牌
【发布时间】:2019-02-15 15:22:42
【问题描述】:

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

我在下面的代码中遇到问题。在没有生成令牌或任何异常的情况下,在下一行之后移动到 finally 块很简单。 "未来的未来 = 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();
}

【问题讨论】:

  • 您可以尝试添加一个 catch 块以确保确实没有异常吗?
  • 授权URI应该是https://login.microsoftonline.com/{tenent id}
  • 资源URI应该是https://graph.microsoft.com
  • 谢谢@juunas 我也试过了,但仍然没有例外。

标签: java spring azure adal adal4j


【解决方案1】:

adal4j-1.5.0的一些依赖好像也没有下载,请查看你项目中的jar包文件。如果我使用 adal4j 1.5.0,基于我的测试。 我发现项目中缺少 adal4j-1.5.0 的一些依赖项。然后我无法获得访问令牌。

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

测试演示代码:

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

【讨论】:

  • 谢谢@汤姆孙。我交叉检查了所有依赖项 jar,发现某些依赖项不存在。我已经添加了这些。它正在工作......非常感谢你......
  • 它对我来说适用于 adal4j 1.5.0。手动添加 oauth2-oidc-sdk-5.24.1 依赖项。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-24
  • 1970-01-01
  • 1970-01-01
  • 2022-01-22
相关资源
最近更新 更多