无法使用 Spring RestTemplate 调用 Salesforce API
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)
您正在尝试将查询字符串参数发送为 headers(这不一样)。下面的代码将等同于你在 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());
}
}
这需要像下面那样实现,效果很好。
// 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("--------------------------------------------");
令牌 url 如下所示。
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=
--------------------------------------------
我正在开发一个应用程序 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)
您正在尝试将查询字符串参数发送为 headers(这不一样)。下面的代码将等同于你在 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());
}
}
这需要像下面那样实现,效果很好。
// 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("--------------------------------------------");
令牌 url 如下所示。
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=
--------------------------------------------