有人可以帮我弄清楚下面的代码有什么问题吗?
我正在使用Spring 3.1.1 RestTemplate尝试在 Box.com 上调用 REST WS以从刷新令牌中获取新的访问令牌。
下面的代码返回一个400 (BAD REQUEST)
. 我能够使用FireFox POST 插件成功调用相同的方法。我已经比较了on the 的输出,它与我从 FireFox 发送的输出完全相同。writeForm method
FormHttpMessageConverter class
有没有人有任何想法?
public static void main(String[] args) throws InterruptedException {
try {
String apiUrl = "https://www.box.com/api/oauth2/token";
String clientSecret = "[MY SECRET]";
String clientId = "[MY ID]";
String currentRefreshToken = "[MY CURRENT VALID REFRESHTOKEN]";
RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(new FormHttpMessageConverter());
restTemplate.setMessageConverters(messageConverters);
MultiValueMap<String, String> body = new LinkedMultiValueMap<String, String>();
body.add("grant_type", "refresh_token");
body.add("refresh_token", currentRefreshToken);
body.add("client_id", clientId);
body.add("client_secret", clientSecret);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
headers.add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8,application/json");
headers.add("Accept-Encoding", "gzip, deflate");
HttpEntity<?> entity = new HttpEntity<Object>(body, headers);
restTemplate.exchange(apiUrl, HttpMethod.POST, entity, String.class);
} catch (Exception ex) {
System.out.println("ex = " + ex.getMessage());
}
}
}