[英]Unable to call Salesforce API using Spring RestTemplate
我正在開發一個 Java + Salesforce 應用程序,並使用 Spring RestTemplate API 調用 salesforce 服務,但似乎出現了一些錯誤。 有什么幫助嗎?
public class RestTemplateExample {
final static String TOKEN_URL = "https://ap5.salesforce.com/services/oauth2/token";
public static void main(String[] args) {
HttpHeaders headers = new HttpHeaders();
headers.add("client_secret", "XXXXXXXXX");
headers.add("client_id", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
headers.add("grant_type", "password");
headers.add("username", "XXX@XX.com");
headers.add("password", "XXXXXXXXXXXX");
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<String> entity = new HttpEntity<String>("", headers);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> result = restTemplate.exchange(TOKEN_URL, HttpMethod.POST, entity, String.class);
System.out.println("TOKEN DETAILS :: "+result.getBody());
}
}
當我通過郵遞員檢查時,它工作正常。
2017-10-14 18:40:35 DEBUG o.s.web.client.RestTemplate - Created POST request for "https://ap5.salesforce.com/services/oauth2/token"
2017-10-14 18:40:35 DEBUG o.s.web.client.RestTemplate - Setting request Accept header to [text/plain, application/json, application/*+json, */*]
2017-10-14 18:40:35 DEBUG o.s.web.client.RestTemplate - Writing [] using [org.springframework.http.converter.StringHttpMessageConverter@2141a12]
2017-10-14 18:40:37 DEBUG o.s.web.client.RestTemplate - POST request for "https://ap5.salesforce.com/services/oauth2/token" resulted in 400 (Bad Request); invoking error handler
Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 400 Bad Request
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:78)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:700)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:653)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:531)
at com.example.RestTemplateExample.main(RestTemplateExample.java:29)
這需要像下面那樣實現,效果很好。
// create the Map of the MultiValueMap
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.add("grant_type", "password");
map.add("client_id", "XX");
map.add("client_secret", "XXX");
map.add("username", "XX");
map.add("password", "XXX");
// RestTemplate
RestTemplate restTemplate = new RestTemplate();
Map<String, String> token = restTemplate.postForObject(TOKEN_URL, map, Map.class);
System.out.println("--------------------------------------------");
System.out.println("Access Token :: "+token.get("access_token"));
System.out.println("Instance Url :: "+token.get("instance_url"));
System.out.println("Id :: "+token.get("id"));
System.out.println("Token_Type :: "+token.get("token_type"));
System.out.println("Signature :: "+token.get("signature"));
System.out.println("--------------------------------------------");
令牌網址如下。
final static String TOKEN_URL = "https://login.salesforce.com/services/oauth2/token";
以下是供參考的輸出:
--------------------------------------------
Access Token :: 00D7F0000001I8v!ARgAQB7R8VtkU8QbUYHfdBjytHPATIDYmMKkPt7sunvUW4DeM2t.c.wFDJ0.k0uNPogX5aPWIrL_lHkrbfNai_t.byjjRCy_
Instance Url :: https://ap5.salesforce.com
Id :: https://login.salesforce.com/id/00D7F0000001I8vUAE/0057F000000l2bgQAA
Token_Type :: Bearer
Signature :: veYhITIlhqf9SJqvBAZ4PKiLh1sj9at13D3m44TnBxw=
--------------------------------------------
您正在嘗試將您的查詢字符串參數作為標頭發送(這是不一樣的)。 以下代碼將等同於您在 Postman 中的測試
public class RestTemplateExample {
final static String TOKEN_URL = "https://ap5.salesforce.com/services/oauth2/token";
public static void main(String[] args) {
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(TOKEN_URL)
.queryParam("client_secret", "XXXXXXXXX")
.queryParam("client_id", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
.queryParam("grant_type", "password")
.queryParam("username", "XXX@XX.com")
.queryParam("password", "XXXXXXXXXXXX");
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<String> entity = new HttpEntity<String>("", headers);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> result = restTemplate.exchange(
builder.build().encode().toUri(),
HttpMethod.POST,
entity, String.class
);
System.out.println("TOKEN DETAILS :: "+result.getBody());
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.