我正在尝试在Spring中使用RestTemplate从服务器获取身份验证令牌。但是,我收到一个400错误的请求。我了解这是我在请求中发送的参数中的问题。我可以使用邮递员成功获取令牌:
Content-Type为application/json
这是我用于请求的代码:
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
OauthRequestUtil oauthRequest = new OauthRequestUtil();
oauthRequest.setClientId(Constants.CLIENT_ID);
oauthRequest.setClientSecret(Constants.CLIENT_SECRET);
oauthRequest.setGrant_type(Constants.GRANTTYPE);
oauthRequest.setResource(Constants.RESOURCE);
HttpEntity<OauthRequestUtil> request = new HttpEntity<>(oauthRequest, headers);
ResponseEntity<String> response = restTemplate.exchange(Constants.OAUTH_URL, HttpMethod.POST, request, String.class);
我在restTempalte.exchange()
行上看到400错误。参数值是正确的,它们是直接从邮递员那里提起的。
OauthRequestUtil
只是一个带有私有变量和getter / setter的类
public class OauthRequestUtil {
private String clientId;
private String clientSecret;
private String grant_type;
private String resource;