0

I am trying to use OAuth2RestOperations to call an api like below and getting "java.lang.IllegalArgumentException: URI is not absolute" exception. What am I missing?

 public void sendSMS(String message, String destination) {
        String messageUrl = "http://baseurl/uri"

        System.out.println("Message url: " + messageUrl);

        JSONObject messageObject = new JSONObject()
            .put("message", message)
            .put("destination", destination);

        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json");
        headers.add("Accept", "application/json");

        HttpEntity<Object> request = new HttpEntity<>(messageObject, headers);

        try {
            ResponseEntity<String> exchange = restTemplate
                .exchange(URI.create(messageUrl), HttpMethod.POST, request, String.class);

            System.out.println("Response is: " + exchange.getBody());
        } catch (Exception e) {
            e.printStackTrace();
        }
Minisha
  • 2,117
  • 2
  • 25
  • 56
  • probably like this https://stackoverflow.com/questions/14848877/uri-not-absolute-exception-getting-while-calling-restful-webservice – Ryuzaki L Jan 30 '20 at 02:36

1 Answers1

0

Found the issue. While configuring the OAuthTemplate I missed to provide the full url.

@Bean
    public OAuth2ProtectedResourceDetails clientCredentialsResourceDetails() {
        ClientCredentialsResourceDetails resource = new ClientCredentialsResourceDetails();
        resource.setAccessTokenUri(
           "http://baseurl/token")      resource.setGrantType("client_credentials");
        resource.setClientId(sapProperties.getAppKey());
        resource.setClientSecret(sapProperties.getAppSecret());
        return resource;
    }
Minisha
  • 2,117
  • 2
  • 25
  • 56