有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用UsernamePasswordCredential提供程序的java列表Azure AD

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

            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: Failed to acquire token with username and password

知道我是否需要在azure active directory中配置一些东西吗


共 (1) 个答案

  1. # 1 楼答案

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

    1. 确保你的账户不是个人账户

    2. 确保启用Allow public client flows

    enter image description here

    1. 使用msal4j库和PublicClientApplication类获取带有用户名、密码和作用域的令牌