0
參考網站:https://developer.salesforce.com/blogs/developer-relations/2017/04/machine-machine-salesforce-integrations-java-rest-soap.html。我開發了下面的代碼,但不幸的是我得到了下面的錯誤。Java Salesforce集成示例不能使用CloseableHttpClient
public class SalesforceRestStarter {
//private static final String TOKEN_URL = "https://login.salesforce.com/services/oauth2/token";
private static final String TOKEN_URL = "https://ap5.salesforce.com/services/oauth2/token";
public static void main(String[] args) throws Exception {
try {
System.out.println("===========================================================================================");
// login
final CloseableHttpClient httpclient = HttpClients.createDefault();
final List<NameValuePair> loginParams = new ArrayList<NameValuePair>();
loginParams.add(new BasicNameValuePair("client_id", "37506609426920XXX"));
loginParams.add(new BasicNameValuePair("client_secret", "3MVG9d8..z.hDcPJZPIzGJ5UZDqJOJY.3R6RBM8sJAF8PyTtdTE.XXXXXXXXXXXXXXXX"));
loginParams.add(new BasicNameValuePair("grant_type", "password"));
loginParams.add(new BasicNameValuePair("username", "[email protected]"));
loginParams.add(new BasicNameValuePair("password", "XXXXXXX"));
final HttpPost post = new HttpPost(TOKEN_URL);
post.setEntity(new UrlEncodedFormEntity(loginParams));
final HttpResponse loginResponse = httpclient.execute(post);
System.out.println("RESPONSE :: ==> "+loginResponse.getStatusLine());
// parse
final ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
final JsonNode loginResult = mapper.readValue(loginResponse.getEntity().getContent(), JsonNode.class);
final String accessToken = loginResult.get("access_token").asText();
final String instanceUrl = loginResult.get("instance_url").asText();
// query contacts
final URIBuilder builder = new URIBuilder(instanceUrl);
builder.setPath("/services/data/v40.0/query/").setParameter("q", "SELECT Id, Name FROM Contact");
final HttpGet get = new HttpGet(builder.build());
get.setHeader("Authorization", "Bearer " + accessToken);
final HttpResponse queryResponse = httpclient.execute(get);
final JsonNode queryResults = mapper.readValue(queryResponse.getEntity().getContent(), JsonNode.class);
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(queryResults));
}
catch (IOException e) {
e.printStackTrace();
}
}
}
但我得到下面的錯誤:
17:22:05,263 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
17:22:05,263 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
17:22:05,263 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/C:/Learnings/salesforce-rest-starter/target/classes/logback.xml]
17:22:05,341 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
17:22:05,356 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
17:22:05,356 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
17:22:05,450 |-WARN in ch.qos.logback.core.ConsoleAppender[STDOUT] - This appender no longer admits a layout as a sub-component, set an encoder instead.
17:22:05,450 |-WARN in ch.qos.logback.core.ConsoleAppender[STDOUT] - To ensure compatibility, wrapping your layout in LayoutWrappingEncoder.
17:22:05,450 |-WARN in ch.qos.logback.core.ConsoleAppender[STDOUT] - See also http://logback.qos.ch/codes.html#layoutInsteadOfEncoder for details
17:22:05,450 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [org.springframework] to DEBUG
17:22:05,450 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [org.springframework] to false
17:22:05,450 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[org.springframework]
17:22:05,450 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [com.example.SalesforceRestStarter] to DEBUG
17:22:05,450 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [com.example.SalesforceRestStarter] to false
17:22:05,450 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[com.example.SalesforceRestStarter]
17:22:05,450 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to ERROR
17:22:05,450 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
17:22:05,450 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
17:22:05,450 |-INFO in [email protected] - Registering current configuration as safe fallback point
RESPONSE :: ==> HTTP/1.1 400 Bad Request
Exception in thread "main" java.lang.NullPointerException
at com.example.SalesforceRestStarter.main(SalesforceRestStarter.java:48)
您是否檢查SalesforceRestStarter類的第48行?錯誤表明有NPE –
NPE - NullPointerException。但是,如果你寫了,那個錯誤是「客戶端標識符無效」,我想建議你確保你使用正確的屬性登錄 - 網址,用戶名,令牌。可能地,不是使用login.salesforce.com,而是使用實際的pod。像na7.salesforce.com。 –
但是,當我通過郵差執行相同的工作。爲什麼雖然Java不工作? – Prateek