使用HttpEntity和HttpHeaders连接到SalesForce

时间:2015-03-22 19:22:54

标签: json spring rest http-headers salesforce

我对此非常陌生,所以请原谅我这个问题是否真的很基本。

我遵循本指南:https://developer.salesforce.com/docs/atlas.en-us.datadotcom_api_dev_guide.meta/datadotcom_api_dev_guide/quickstart_connectwithoauth.htm

我想获得一个访问令牌,但我一直得到400个错误的请求。我在这里做错了什么?

String uri = "https://na1.salesforce.com/services/oauth2/token";

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("client_id", "some_id");
headers.set("client_secret", "secret_thing");
headers.set("grant_type", "password");
headers.set("username", "username@foo.com");
headers.set("password", "password_here");

HttpEntity<String> request = new HttpEntity<String> (headers);

System.out.println(new RestTemplate().exchange(uri, HttpMethod.GET,request, Object.class));

上述代码有什么问题?我一直得到400个不好的请求而不是json blob,这正是我想要的。

以下是我不断收到的错误:

 15/03/22 12:55:35 WARN client.RestTemplate: GET request for "https://na1.salesforce.com/services/oauth2/token" resulted in 400 (Bad Request); invoking error handler

org.springframework.web.client.HttpClientErrorException: 400 Bad Request
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:88)
    at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:532)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:488)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:446)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:393)

0 个答案:

没有答案