0

我正在使用 adal4j J​​ava 库通过 Azure Active Directory 对 Azure DevOps REST API 调用进行身份验证。我可以使用个人访问令牌进行身份验证,但不能使用 Active Directory。这是我一直在运行的代码:

AuthenticationResult result = null;
ExecutorService service = null;
try {
  service = Executors.newFixedThreadPool(1);
  AuthenticationContext context = new AuthenticationContext("https://login.microsoftonline.com/" + tenantId + "/", false, service);
  if (clientId != null && clientKey != null) {
    ClientCredential credentials = new ClientCredential(clientId, clientKey);
    Future<AuthenticationResult> future = context.acquireToken("499b84ac-1321-427f-aa17-267ca6975798", credentials, null);
    result = future.get();
  }
} finally {
  if (service != null) {
    service.shutdown();
  }
}

我收到以下错误:

StatusCode: 203, ReasonPhrase: '非权威信息'

它尝试重定向到此登录页面:

https://spsprodsin1.vssps.visualstudio.com/_signin?realm= ...

我已经使用正确的目录/租户从 Azure DevOps 组织设置连接到 Azure Active Directory。而且我还在应用注册中添加了 Azure DevOps user_impersonation 权限(委托)。

我在这里做错了什么,我该如何解决这个问题?

4

1 回答 1

0

在此处输入图像描述

user_impersonation表示此 API 只能在用户权限下调用。但是,您获得的令牌带有仅具有应用程序权限的客户端凭据。

要为用户获取令牌,您可以尝试以下代码:

    public static AuthenticationResult GetToeknWithPasswordForDevOps(String username, String password){
        ExecutorService service = Executors.newFixedThreadPool(1);
        AuthenticationContext context = null;
        AuthenticationResult result = null;
        try {
            context = new AuthenticationContext(AUTHORITY, true, service);
            Future<AuthenticationResult> future = context.acquireToken("499b84ac-1321-427f-aa17-267ca6975798", "{your publuc app client id}", username, password, null);
            result = future.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } finally {
            service.shutdown();
        }
        return result;
    }
于 2019-11-15T09:20:54.473 回答