2017-10-14 91 views
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) 

enter image description here

+0

您是否檢查SalesforceRestStarter類的第48行?錯誤表明有NPE –

+0

NPE - NullPointerException。但是,如果你寫了,那個錯誤是「客戶端標識符無效」,我想建議你確保你使用正確的屬性登錄 - 網址,用戶名,令牌。可能地,不是使用login.salesforce.com,而是使用實際的pod。像na7.salesforce.com。 –

+0

但是,當我通過郵差執行相同的工作。爲什麼雖然Java不工作? – Prateek

回答

0

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