我正在尝试使用OAuth2RestOperations调用如下所示的api,并获取“ java.lang.IllegalArgumentException:URI不是绝对的”异常。我想念什么?
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();
}
最佳答案
找到了问题。在配置OAuthTemplate时,我错过了提供完整网址的机会。
@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;
}