是否可以将 DeviceCode 身份验证流程与 Azure Java SDK 一起使用?

Is it possible to use the DeviceCode authentication Flow with Azure Java SDK?

我使用 azure msal4j 库成功生成了一个 IAuthenticationResult - 我看到了一个设备代码,当将该代码输入浏览器时,它显示了正确的范围/权限, 现在我想获取此身份验证结果并将其传递到 Azure-SDK 身份验证中,类似于:

    val result = DeviceCodeFlow.acquireTokenDeviceCode()


    val a: Azure = Azure.configure()
        .withLogLevel(LogLevel.BODY_AND_HEADERS)
        .authenticate(AzureCliCredentials.create(result))
        .withDefaultSubscription()

有谁知道在哪里可以查看/或执行此操作的任何样本?

如果要使用msal4j库获取访问令牌,然后使用令牌通过Azure管理SDK管理Azure资源,请参考以下代码

public class App {
    public static void main(String[] args) throws Exception {
        String subscriptionId = ""; // the subscription id
        String domain="";// Azure AD tenant domain 
        DeviceCodeTokenCredentials tokencred = new DeviceCodeTokenCredentials(AzureEnvironment.AZURE,domain);
         Azure azure =Azure.configure()
                           .withLogLevel(LogLevel.BASIC)
                           .authenticate(tokencred)
                           .withSubscription(subscriptionId);
                                  
         for(AppServicePlan plan : azure.appServices().appServicePlans().list()) {
                  
                  System.out.println(plan.name());
                  
                  }
    }  
}

// define a class to extend AzureTokenCredentials
 class DeviceCodeTokenCredentials extends AzureTokenCredentials{

    public DeviceCodeTokenCredentials(AzureEnvironment environment, String domain) {
        super(environment, domain);
    }

    @Override
    public String getToken(String resource) throws IOException {
        // use msal4j to get access token 
        String clientId="d8aa570a-68b3-4283-adbe-a1ad3c1dfd8d";// you Azure AD application app id
        String AUTHORITY = "https://login.microsoftonline.com/common/";
        Set<String> SCOPE = Collections.singleton("https://management.azure.com/user_impersonation");
        PublicClientApplication pca = PublicClientApplication.builder(clientId)
                .authority(AUTHORITY)
                .build();

        Consumer<DeviceCode> deviceCodeConsumer = (DeviceCode deviceCode) ->
        System.out.println(deviceCode.message());

      DeviceCodeFlowParameters parameters =
        DeviceCodeFlowParameters
                .builder(SCOPE, deviceCodeConsumer)
                .build();
      IAuthenticationResult result = pca.acquireToken(parameters).join();       
      return result.accessToken();
    } 
 }