2017-10-14 6 views
0

En se référant au site: https://developer.salesforce.com/blogs/developer-relations/2017/04/machine-machine-salesforce-integrations-java-rest-soap.html. J'ai développé le code ci-dessous mais malheureusement je reçois l'erreur ci-dessous.Exemple d'intégration Java Salesforce ne fonctionne pas avec 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(); 
     } 
    } 
} 

Mais je reçois l'erreur suivante:

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) 

enter image description here

+0

Vérifiez-vous la ligne 48 de la classe SalesforceRestStarter? L'erreur indique clairement qu'il existe NPE –

+0

NPE - NullPointerException. Mais si vous avez écrit, cette erreur est "identifiant client invalide", je voudrais vous suggérer de vous assurer que vous utilisez les bonnes propriétés pour la connexion - url, nom d'utilisateur, jeton. Éventuellement, au lieu d'utiliser login.salesforce.com, utilisez le module réel. Quelque chose comme na7.salesforce.com. –

+0

Mais quand j'exécute le même par Postman, ça marche. pourquoi si java ne fonctionne pas? – Prateek

Répondre

0

La client_id et la valeur Client_secrete besoin de mélanger. Ça fonctionne bien.

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