使用Azure java sdk对Azure进行身份验证

使用Azure java sdk对Azure进行身份验证,azure,azure-authentication,azure-java-sdk,Azure,Azure Authentication,Azure Java Sdk,这是检查客户端、租户和密钥是否有效的正确方法吗? 那么这段代码需要哪些JAR,在哪里下载 String client = "xxxxxxxxxxx"; String tenant = "xxxxxxxxxxx"; String key = "xxxxxxxxxxx"; String subscriptionId = "xxxxxxxxxxx"; ApplicationTokenCredentials credentials = new ApplicationTokenCredentials(c

这是检查客户端、租户和密钥是否有效的正确方法吗? 那么这段代码需要哪些JAR,在哪里下载

String client = "xxxxxxxxxxx";
String tenant = "xxxxxxxxxxx";
String key = "xxxxxxxxxxx";
String subscriptionId = "xxxxxxxxxxx";

ApplicationTokenCredentials credentials = new ApplicationTokenCredentials(client, tenant,key, AzureEnvironment.AZURE);
Azure azure = Azure.configure().authenticate(credentials).withDefaultSubscription();
azure.subscriptions().list();

如果您想获得访问令牌,可以尝试以下方法

pom.xml中的依赖项如下所示,或加载jar:

有关更多详细信息,请参阅


401错误与您的权限有关。需要范围https://management.azure.com/.default.

我尝试了这个方法,但出现了很多错误-谢谢,我解决了错误。但通过使用从上述代码接收的访问令牌无法获取订阅,我得到了401个错误,因为RESTAPI使用访问代码获取订阅。是否需要进行任何更改?
private static IAuthenticationResult getAccessTokenByClientCredentialGrant() throws Exception {

        ConfidentialClientApplication app = ConfidentialClientApplication.builder(
                CONFIDENTIAL_CLIENT_ID,
                ClientCredentialFactory.createFromSecret(CONFIDENTIAL_CLIENT_SECRET))
                .authority(TENANT_SPECIFIC_AUTHORITY)
                .build();

        // With client credentials flows the scope is ALWAYS of the shape "resource/.default", as the
        // application permissions need to be set statically (in the portal), and then granted by a tenant administrator
        ClientCredentialParameters clientCredentialParam = ClientCredentialParameters.builder(
                Collections.singleton(GRAPH_DEFAULT_SCOPE))
                .build();

        CompletableFuture<IAuthenticationResult> future = app.acquireToken(clientCredentialParam);
        return future.get();
    }
    <dependency>
        <groupId>com.microsoft.azure</groupId>
        <artifactId>msal4j</artifactId>
        <version>1.2.0</version>
    </dependency>