可以参考以下代码
public void run(String... args) throws Exception {
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(StandardCharsets.UTF_8);
RestTemplate restTemplate = new RestTemplateBuilder()
.setConnectTimeout(Duration.ofSeconds(60))
.additionalMessageConverters(stringHttpMessageConverter)
.build();
String uri = "https://api.paypal.com/v1/oauth2/token?grant_type=client_credentials";
String username = "yourAppClientId";
String password = "yourAppPwd";
HttpHeaders basicAuth = new HttpHeaders() {{
String auth = username + ":" + password;
byte[] encodedAuth = Base64.encodeBase64(
auth.getBytes(StandardCharsets.US_ASCII));
String authHeader = "Basic " + new String(encodedAuth);
set("Authorization", authHeader);
}};
ResponseEntity<String> response = restTemplate.exchange
(uri, HttpMethod.POST, new HttpEntity<>(basicAuth), String.class);
System.out.println(response.getBody());
}