Java Salesforce 集成示例无法使用 CloseableHttpClient
Java Salesforce Integration example not working using CloseableHttpClient
通过参考站点:https://developer.salesforce.com/blogs/developer-relations/2017/04/machine-machine-salesforce-integrations-java-rest-soap.html。我开发了以下代码,但不幸的是我收到以下错误。
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", "XXXXXXX@xy.com"));
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 ch.qos.logback.classic.joran.JoranConfigurator@28f67ac7 - 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)
client_id 和 Client_secrete 值需要随机排列。它工作正常。
RESPONSE :: ==> HTTP/1.1 200 OK
LOGIN RESULT :: {"access_token":"00D7F0000001I8v!ARgAQAf1lg.VhKTpe3NVvw3BMkzlioEwH868CpPxGoat7VzZXL2MWiue7m_UNPtM8.mYDq_xwko.SdU4GD8TPvW8AcJaiSN6","instance_url":"https://ap5.salesforce.com","id":"https://login.salesforce.com/id/00D7F0000001I8vUAE/0057F000000l2bgQAA","token_type":"Bearer","issued_at":"1507984848402","signature":"le7en7D2y/iaIOywRMNf/lQ/mX4Iw48TGA5zc/TXEvg="}
{
"totalSize" : 60,
"done" : true,
"records" : [ {
"attributes" : {
"type" : "Contact",
"url" : "/services/data/v39.0/sobjects/Contact/0037F0000020XkCQAU"
},
"Id" : "0037F0000020XkCQAU",
"Name" : "Rose Gonzalez"
}, {
"attributes" : {
"type" : "Contact",
"url" : "/services/data/v39.0/sobjects/Contact/0037F0000020XkDQAU"
},
"Id" : "0037F0000020XkDQAU",
"Name" : "Sean Forbes"
},{
"attributes" : {
"type" : "Contact",
"url" : "/services/data/v39.0/sobjects/Contact/0037F000001l7seQAA"
},
"Id" : "0037F000001l7seQAA",
"Name" : "Kiran Khot"
} ]
}
通过参考站点:https://developer.salesforce.com/blogs/developer-relations/2017/04/machine-machine-salesforce-integrations-java-rest-soap.html。我开发了以下代码,但不幸的是我收到以下错误。
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", "XXXXXXX@xy.com"));
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 ch.qos.logback.classic.joran.JoranConfigurator@28f67ac7 - 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)
client_id 和 Client_secrete 值需要随机排列。它工作正常。
RESPONSE :: ==> HTTP/1.1 200 OK
LOGIN RESULT :: {"access_token":"00D7F0000001I8v!ARgAQAf1lg.VhKTpe3NVvw3BMkzlioEwH868CpPxGoat7VzZXL2MWiue7m_UNPtM8.mYDq_xwko.SdU4GD8TPvW8AcJaiSN6","instance_url":"https://ap5.salesforce.com","id":"https://login.salesforce.com/id/00D7F0000001I8vUAE/0057F000000l2bgQAA","token_type":"Bearer","issued_at":"1507984848402","signature":"le7en7D2y/iaIOywRMNf/lQ/mX4Iw48TGA5zc/TXEvg="}
{
"totalSize" : 60,
"done" : true,
"records" : [ {
"attributes" : {
"type" : "Contact",
"url" : "/services/data/v39.0/sobjects/Contact/0037F0000020XkCQAU"
},
"Id" : "0037F0000020XkCQAU",
"Name" : "Rose Gonzalez"
}, {
"attributes" : {
"type" : "Contact",
"url" : "/services/data/v39.0/sobjects/Contact/0037F0000020XkDQAU"
},
"Id" : "0037F0000020XkDQAU",
"Name" : "Sean Forbes"
},{
"attributes" : {
"type" : "Contact",
"url" : "/services/data/v39.0/sobjects/Contact/0037F000001l7seQAA"
},
"Id" : "0037F000001l7seQAA",
"Name" : "Kiran Khot"
} ]
}