为Azure Api管理休息Api生成令牌

时间:2017-06-21 06:06:33

标签: java rest azure adal azure-api-management

我尝试使用Adal 4 Java调用Azure API Management Rest Api并基于以下示例:https://blogs.msdn.microsoft.com/azureossds/2015/06/23/authenticating-azure-resource-management-rest-api-requests-using-java/

该代码适用于我从API Management UI中获取预生成的Access-Token的部分,但是当我尝试以编程方式执行时,我失败了。

工作代码:

String accessToken = "sometoken";
HttpGet request = new HttpGet("https://testapics.management.azure-api.net/apis/subscription/?api-version=2017-03-01");
request.addHeader("Authorization", "Bearer " + accessToken);
response = client.execute(request);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

与示例相关的标识符,主要和辅助密钥是什么。我是否仍需要将其余的api注册为Azure AD中的应用程序并为其创建服务主体?

这是失败的部分:

private AuthenticationResult getAccessTokenFromUserCredentials() throws Exception {
    AuthenticationContext context = null;
    AuthenticationResult result = null;
    ExecutorService service = null;
    try {
        service = Executors.newFixedThreadPool(1);
        context = new AuthenticationContext(AUTHORITY, false, service);

        ClientCredential credential = new ClientCredential("integration", "passwort");
        Future<AuthenticationResult> future = context.acquireToken("https://management.azure.com/", credential, null);
        result = future.get();
    } finally {
        service.shutdown();
    }
    if (result == null) {
        throw new ServiceUnavailableException("authentication result was null");
    }
    return result;
}

我总是得到 com.microsoft.aad.adal4j.AuthenticationException:{“error_description”:“AADSTS70001:在尝试使用令牌生成执行时,在目录中找不到具有标识符'集成'的应用程序

由于

2 个答案:

答案 0 :(得分:0)

错误消息告诉您需要在Azure AD中注册已注册的应用的客户端ID

ClientCredential credential = new ClientCredential("client-id-guid-here", "client-secret-goes-here");

您还需要将客户的秘密提供给上述应用程序。

答案 1 :(得分:0)

我自己想出了问题。有一个关于如何通过MAC SHA512 Hash for C#自己创建accessToken的例子,我成功地将它移植到了java。因此,与admin用户保持联系时无需调用授权服务器。