0
votes

I am going to call Azure Resource Management Rest API from my Spring Boot application. For that i need to have authorization token. After googling thorough different sources, i could understand that we need to call POST API to generate Authorization token using below URL;

https://login.microsoftonline.com/{tenant_id}/oauth2/token

with below request body, Below details i have got by creating service principle and Active directory

client_secret resource
grant_type
tenant_id

Spring boot java code:

    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    String url= "https://login.microsoftonline.com/{tenant_id}/oauth2/token";
    AuthTokenBody authTokenBody = service.setBody();
    headers.setContentType(MediaType.APPLICATION_JSON);
    restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
    HttpEntity<AuthTokenBody> entity = new HttpEntity<>(authTokenBody, headers);
    ResponseEntity response= restTemplate.postForObject(url, entity, ResponseEntity.class);

But i get below error:

{ "timestamp": "2018-12-23T11:52:58.175+0000", "status": 500, "error": "Internal Server Error", "message": "400 Bad Request", "path": "/cdaas/app" }

Can you please guide how to call generate Authentication code in Spring-boot to cal Azure Resource management.

Thank you in advance

1
Hi,does my answer helps you?Jay Gong

1 Answers

1
votes

Please refer to below code, it works for me.

import com.microsoft.aad.adal4j.AuthenticationContext;
import com.microsoft.aad.adal4j.AuthenticationResult;
import com.microsoft.aad.adal4j.ClientCredential; // for service principal

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class GetAuthCode {
    public static void main(String[] args) {
        // Account specific values
        String tenantId = "***";
        String clientId = "***";
        String password = "***";

        // use adal to Authenticate
        AuthenticationContext authContext = null;
        AuthenticationResult authResult = null;
        ExecutorService service = null;

        try {
            service = Executors.newFixedThreadPool(1);
            String url = "https://login.microsoftonline.com/" + tenantId + "/oauth2/authorize";
            authContext = new AuthenticationContext(url,
                    false,
                    service);
            ClientCredential clientCred = new ClientCredential(clientId, password);
            Future<AuthenticationResult> future = authContext.acquireToken(
                    "https://management.azure.com/",
                    clientCred,
                    null);
            authResult = future.get();
            System.out.println(authResult.getAccessToken());
        } catch (Exception ex) {
            // handle exception as needed
        } finally {
            service.shutdown();
        }
    }
}

More details,please navigate to this doc.