简体   繁体   中英

OAuth2RestOperations: java.lang.IllegalArgumentException: URI is not absolute

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();
        }

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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM