0
votes

I am trying to Make PUT request to craete project in azure migrate with spring boot .Previously i was doing with Rest Template. Like @Autowire RestTemplate template;

HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

    MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
    map.add("grant_type", assessment.getGrant_type());
    map.add("client_id", assessment.getClient_id());
    map.add("client_secret", assessment.getClient_secret());
    map.add("code", authToken);

    HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
    ResponseEntity<String> response = restTemplate.exchange(baseUrl, HttpMethod.POST, request, String.class);

authToken i am getting making separate GET call to Authentication URL previous to POST call and setting in POST header.

I am posting partial code so that you can get an idea what i am doing.

Now i have been asked to use Azure-sdk for java to write the same code. I need not to use Rest Template.Can anybody tell me how can i make POST call using Azure-sdk for java classes.I search on google and you tube but nothing concrete found.

Regards, Prabhash Mishra

1
Can any one give sample code as it is vary urgent.Prabhash Mishra
Did you succeed in using Rest API with Azure Migrate service ? If so can you please upload sample code in answer.srinannapa

1 Answers

0
votes

If you want to use Azure java sdk to call Azure rest API, please refer to the following code

a. create a service principal (I use Azure CLI to do that)

az login
az account set --subscription "<your subscription id>"
# the sp will have Azure Contributor role
az ad sp create-for-rbac -n "readMetric" 

enter image description here

  1. Install sdk
<!-- https://mvnrepository.com/artifact/com.microsoft.azure/azure -->
<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure</artifactId>
    <version>1.33.0</version>
</dependency>

  1. Code
 private static String tenantId="hanxia.onmicrosoft.com"; // sp tenant
    private static String clientId = "42e0d080-b1f3-40cf-8db6-c4c522d988c4"; // sp appid

    private static String clientKey = "Gbx*************JDfQpIjoae:";// sp password
    private static String subscriptionId="e5b0fcfa-e859-43f3-8d84-5e5fe29f4c68"; //sp subscription id

ApplicationTokenCredentials creds = new
                ApplicationTokenCredentials(clientId,tenantId,clientKey, AzureEnvironment.AZURE);

        RestClient restClient =new RestClient.Builder()
                .withBaseUrl(AzureEnvironment.AZURE, AzureEnvironment.Endpoint.RESOURCE_MANAGER)
                .withSerializerAdapter(new AzureJacksonAdapter())
                .withReadTimeout(150, TimeUnit.SECONDS)
                .withLogLevel(LogLevel.BODY)
                .withResponseBuilderFactory(new AzureResponseBuilder.Factory())
                .withCredentials(creds)
                .build();
        OkHttpClient httpClient = restClient.httpClient().newBuilder().build();
        String url="https://management.azure.com/subscriptions/"+subscriptionId+"/providers/Microsoft.Migrate/projects?api-version=2018-02-02";
        Request request = new Request.Builder()
                              .url(url)
                              .method("get",null)
                              .build();
        Response response1 = httpClient.newCall(request).execute();
        if(response1.isSuccessful()){

            System.out.println(response1.headers().toString());

        }

enter image description here