简体   繁体   中英

Java Salesforce Integration example not working using CloseableHttpClient

By referring to the site : https://developer.salesforce.com/blogs/developer-relations/2017/04/machine-machine-salesforce-integrations-java-rest-soap.html . I developed the below code but unfortunately I am getting the below error.

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();
        }
    }
}

But I get the below error:

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)

在此处输入图片说明

The client_id and Client_secrete value need to shuffle. It works fine.

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"
  } ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM