Met office api calling via java code returns 401 code the same credentials works fine in postman

14 Views Asked by At

I was trying to explore the usage of the Met office specific location api data and tried calling in java code with below snippet and receiving an error code of 401 though the same credentials works fine in postman.

Java code:

public void cronJobSchDaily() {
        try {
            Number longitude = -4.56999;
            Number latitude = 52.13914;
            final String url = "https://api-metoffice.apiconnect.ibmcloud.com/v0/forecasts/point/daily?longitude=" +longitude +"&latitude=" + latitude;
            RestTemplate restTemplate = new RestTemplate();
            HttpHeaders header = new HttpHeaders();
            header.set("apikey", Constants.APIKEY);
            header.set(HttpHeaders.ACCEPT, "application/json");

            HttpEntity<String> requestEntity = new HttpEntity<String>("body",header);
            ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class);
            System.out.println("Response code"+ responseEntity.getStatusCode());
            }catch (Exception e) {
            e.printStackTrace();
           }

         }
}

Expecting to return the weather data as shown below.enter image description here

attaching failure 401 code. enter image description here

0

There are 0 best solutions below