OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

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

  • Thread starter Thread starter Vamsi Krishna
  • Start date Start date
V

Vamsi Krishna

Guest
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:

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
<p>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.</p>
<p>Java code:</p>
<pre><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();
}

}
}
</code></pre>
<p>Expecting to return the weather data as shown below.<a href="https://i.sstatic.net/PWpEW.png" rel="nofollow noreferrer">enter image description here</a></p>
<p>attaching failure 401 code. <a href="https://i.sstatic.net/17Y1J.png" rel="nofollow noreferrer">enter image description here</a></p>
I have found how to fix this.
 

Latest posts

Top