首页 > 解决方案 > 使用 UsernamePasswordCredential 提供程序列出 Azure AD

问题描述

我正在使用 UsernamePasswordCredential 提供程序连接到 AAD 并使用 msgraph-sdk-java ( https://github.com/microsoftgraph/msgraph-sdk-java ) 获取用户,代码如下:

            final UsernamePasswordCredential usernamePasswordCredential = new UsernamePasswordCredentialBuilder()
                .clientId("client_id")
                .username("user_name")                             
                .password("password")
                .build();

        final TokenCredentialAuthProvider tokenCredentialAuthProvider = new TokenCredentialAuthProvider(Arrays.asList("https://graph.microsoft.com/.default"), usernamePasswordCredential);

        final GraphServiceClient graphClient =
          GraphServiceClient
            .builder()
            .authenticationProvider(tokenCredentialAuthProvider)
            .buildClient();

        UserCollectionPage userCollectionpage = graphClient.users().buildRequest().get();

我收到以下错误:

java.io.IOException:java.util.concurrent.ExecutionException:com.azure.core.exception.ClientAuthenticationException:无法使用用户名和密码获取令牌

知道我是否需要在天蓝色的活动目录中配置一些东西吗?

标签: javaazureazure-active-directoryazure-web-app-servicemicrosoft-graph-sdks

解决方案


您的问题已通过评论解决。将其发布为结束线程的答案:

  1. 确保您的帐户不是个人帐户。

  2. 确保启用Allow public client flows.

在此处输入图像描述

  1. 使用 msal4j 库和 PublicClientApplication 类获取具有用户名、密码和范围的令牌。

推荐阅读