0

I am studying the azure java sdk and I did not find a wrapper method for

az account get-access-token --resource api://xxxxx.microsoft.com/YYYY/ZZZZ

This request works properly from windows batch but i cannot run using azure java sdk. I'll appreciate any feedback about how to run this command using azure java sdk

Best Regards, Aurelian

1 Answer 1

1

azure java sdk for az get token programatically:-

You can use the below java spring-boot code to retrieve the access token and it worked for me as follows.

RestApiController.java

package com.myapp.restcall.controller;  
import java.util.Collections;  
import org.json.JSONObject;  
import org.springframework.http.HttpEntity;  
import org.springframework.http.HttpHeaders;  
import org.springframework.http.HttpMethod;  
import org.springframework.http.MediaType;  
import org.springframework.http.ResponseEntity;  
import org.springframework.util.LinkedMultiValueMap;  
import org.springframework.util.MultiValueMap;  
import org.springframework.web.bind.annotation.GetMapping;  
import org.springframework.web.bind.annotation.RestController;  
import org.springframework.web.client.RestTemplate;  
  
@RestController  
public class RestApiController {  
  
   @GetMapping(value = "/gettoken")  
   public static String getToken() throws Exception {  
   String url = "https://login.microsoftonline.com/<tenantID>/oauth2/token";  
  HttpHeaders headers = new HttpHeaders();  
  headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);  
  MultiValueMap<String, String> inputMap = new LinkedMultiValueMap<>();  
  
  inputMap.add("grant_type", "client_credentials");  
  inputMap.add("client_id", "CLIENT_ID");  
  inputMap.add("client_secret", "CLIENT_SECRET");  
  inputMap.add("resource", "https://management.core.windows.net/");  
  HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<MultiValueMap<String, String>>(inputMap,  
  headers);  
  RestTemplate template = new RestTemplate();  
  ResponseEntity<String> response = template.postForEntity(url, entity, String.class);  
  JSONObject myjson = new JSONObject(response.getBody());  
  String data = (String) myjson.get("access_token");  
  System.out.println(data);  
  String access_token = data;  
 return access_token;  
  
  }  
}

Replace CLIENT_ID, CLIENT_SECRET & TENANT_ID in the above java code before executing it.

To build the program, use below commands in the IDE terminal.

mvn clean install
mvn spring-boot:run

enter image description here

Token generated at localhost:8080/gettoken successfully.

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you @Jahnavi, the main issue which I have is that I am not able to get that client secret, I will test you code and I will let you know.
Goto Azure Active Directory -> Search for App registrations -> create a new registration and then you will be able to see the certificates and secrets options under manage and create a new client secret as shown. Copy the secret value and add it in the above code. @aurelianr
unfortunately i don t have access in that zone in my company azure cloud,
If you need to use azure java sdk, you have to create an appid and secret for sure. It is not possible to authenticate without these. @aurelianr

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.