正如 Łukasz Olszewski 所说,工作正常:
String params = Map.of(
Constants.PARAM_CLIENT_ID, apiObject.getClientId(),
Constants.PARAM_SCOPE, apiObject.getScope(),
Constants.PARAM_CODE, apiObject.getCode(),
Constants.PARAM_REDIRECT_URI, apiObject.getRedirectUri(),
Constants.PARAM_GRANT_TYPE, apiObject.getGrantType(),
Constants.PARAM_CODE_VERIFIER, apiObject.getCodeVerifier())
.entrySet()
.stream()
.map(entry -> Stream.of(
URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8),
URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8))
.collect(Collectors.joining("="))
).collect(Collectors.joining("&"));
HttpResponse<?> response = utils.consumeHttpPostFormUrlEncodedClientByRequestUrl(Constants.URL_BASE + Constants.URL_GET_TOKEN, params);
并使用HttpPostFormUrlEncodedClientByRequestUrl
public HttpResponse<?> consumeHttpPostFormUrlEncodedClientByRequestUrl(String url, String map) throws IOException, InterruptedException {
HttpClient httpClient = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
.header("Content-Type", String.valueOf(MediaType.APPLICATION_FORM_URLENCODED))
.POST(HttpRequest.BodyPublishers.ofString(map))
.build();
return httpClient.send(request, HttpResponse.BodyHandlers.ofString());
}