简体   繁体   English

为Azure Api管理Rest Api生成令牌

[英]Generating Token for Azure Api Management Rest Api

I am trying to call the Azure API Management Rest Api with Adal 4 Java and based on following example: https://blogs.msdn.microsoft.com/azureossds/2015/06/23/authenticating-azure-resource-management-rest-api-requests-using-java/ 我正在尝试使用Adal 4 Java并基于以下示例调用Azure API Management Rest Api: https//blogs.msdn.microsoft.com/azureossds/2015/06/23/authenticating-azure-resource-management-rest -API-请求-使用的Java /

The code works fine for the part where I take the pregenerated Access-Token from the API Management UI, but I fail when I try to do it programatically. 该代码对于我从API Management UI中获取预生成的访问令牌的部分而言效果很好,但是当我尝试以编程方式进行操作时却失败了。

Working code: 工作代码:

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()));

What are the Identifier, Primary and Secondary key needed for in relation to the example. 与示例相关的标识符,主键和辅助键是什么。 Do I still have to register the rest api as an application in Azure AD and create a service principal for it? 我是否还必须将其余api注册为Azure AD中的应用程序,并为其创建服务主体?

This is the failing part: 这是失败的部分:

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;
}

I always get com.microsoft.aad.adal4j.AuthenticationException: {"error_description":"AADSTS70001: Application with identifier 'integration' was not found in the directory when trying to execute with token generation. 我总是得到com.microsoft.aad.adal4j.AuthenticationException:{“ error_description”:“ AADSTS70001:尝试执行令牌生成时,在目录中找不到具有标识符“集成”的应用程序

Thanks 谢谢

The error message is telling you that you need to put the client id of the app that you have registered in Azure AD here: 错误消息告诉您,您需要在此处放置已在Azure AD中注册的应用程序的客户端ID

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

You also need to put the client secret of the afore-mentioned app there. 您还需要将上述应用程序的客户端密码放在此处。

I figured out the problem myself. 我自己解决了这个问题。 There is an example on how to create the accessToken yourself via MAC SHA512 Hash for C# and I successfully ported it to java. 有一个示例,说明如何通过MAC SHA512 Hash for C#自己创建accessToken,我已成功将其移植到java。 So there is no need to call the Authorization Server when staying with the admin user. 因此,与管理员用户在一起时无需调用授权服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM