如何使用基于管理证书的身份验证对Azure进行REST API调用?

时间:2015-10-05 10:24:04

标签: java azure adal azure-billing-api

我正在尝试使用Java应用程序从Microsoft Azure获取使用率和价目表信息,我开始明白我可以使用管理证书对Microsoft Azure进行身份验证进行身份验证。

我从here获得的.publishsettings文件中获得了管理证书

但是,在AuthenticationContext中,我没有看到任何使用此证书的方法来获取使用和评价API调用所需的访问令牌。

我尝试引用this answer,但我没有看到任何可用的客户端和价目表,答案是指管理客户端,而不是我的用例。我也引用了这个博客,其中引用了ClientAssertionCertificate,我在java library for adal中没有看到。

NB:我可以使用用户名,密码和&amp ;;对Azure进行REST API调用,以获取使用情况和价目表信息。基于客户端ID的身份验证机制,但我想利用此管理证书机制,因为我的应用程序的用户可能不信任此应用程序及其凭据,并且从用户角度来看,这种基于证书的机制似乎更容易使用。 / p>

2 个答案:

答案 0 :(得分:1)

简单回答是您无法使用管理证书来使用结算API。 Billing API实质上是使用Azure AD令牌的新API的一部分。

管理证书只能用于Service Management APIs

答案 1 :(得分:1)

  

但是,在AuthenticationContext中,我没有看到任何利用此证书获取使用和评价API调用所需的访问令牌的方法。

     

我也引用了这个博客,它引用了ClientAssertionCertificate,我在java库中看不到adal。

正如Gaurav所说,我们只能打电话给用户&使用Azure Active Directory进行身份验证的评级卡API。您可以使用AuthenticationContext获取access_token,如下面的代码。您需要提供client IDClient Secretkey)。

private AuthenticationResult getAccessTokenFromClientCredentials()
            throws Throwable {
        AuthenticationContext context = null;
        AuthenticationResult result = null;
        ExecutorService service = null;
        try {
            service = Executors.newFixedThreadPool(1);
            context = new AuthenticationContext(authority + tenant + "/", true,
                    service);
            Future<AuthenticationResult> future = context.acquireToken(
                    "https://graph.windows.net", new ClientCredential(clientId,
                            clientSecret), null);
            result = future.get();
        } catch (ExecutionException e) {
            throw e.getCause();
        } finally {
            service.shutdown();
        }

        if (result == null) {
            throw new ServiceUnavailableException(
                    "authentication result was null");
        }
        return result;
    }
  

注意:我可以使用用户名,密码和密码对Azure进行REST API调用,以获取使用情况和价目表信息。基于客户端ID的认证机制,.....

似乎我们无法使用管理证书机制来调用Usage&amp;价目表API。因为这些调用用户或服务主体是Azure AD租户中Owner, Contributor or Reader role的成员,用于请求的订阅(see this document)。我建议您参考此文档,了解如何authenticate Azure Resource Management

相关问题