2016-03-08 31 views
0

所以我不確定是否有很多人使用過BrightPattern API,但我需要做的是能夠使用HTTP Post和JSON發佈記錄。現在我發佈到我的網址,我回來401錯誤,當我正在尋找204.我有憑據,並且我知道401是一個錯誤,可能意味着憑據不正確,或你需要登錄或其他東西。我會告訴你們我的代碼,看看你們是否能看到我的問題。BrightPattern API 401錯誤,使用Java和JSON添加記錄

另外我真的很需要這個想法,所以我會盡快提供賞金,只要我能得到正確的答案。

public class GSONBrightPatternPost { 

public static void main(String[] args) { 

    Person personObj = new Person(); 
    personObj.setOrganization("ACC"); 
    personObj.setFirstName("Harry"); 
    personObj.setLastName("Smith"); 
    personObj.setPhone1(null); 
    personObj.setPhone2(null); 
    personObj.setEmail(null); 
    personObj.setState("AZ"); 
    personObj.setLeadDate(null); // Fix Later 
    personObj.setCompany("Bob's Mortage"); 
    personObj.setCompanyContactName("Indiana Jones"); 
    personObj.setOutsideRep("Joel Martin"); 

    Person personObj2 = new Person(); 
    personObj2.setOrganization("ACC"); 
    personObj2.setFirstName("Richard"); 
    personObj2.setLastName("Nixon"); 
    personObj2.setPhone1(null); 
    personObj2.setPhone2(null); 
    personObj2.setEmail(null); 
    personObj2.setState(null); 
    personObj2.setLeadDate(null); // Fix Later 
    personObj2.setCompany("Bob's Mortage"); 
    personObj2.setCompanyContactName("Indiana Jones"); 
    personObj2.setOutsideRep("Joel Martin"); 

    List<Person> personArrayList = new ArrayList<Person>(); 
    personArrayList.add(personObj); 
    personArrayList.add(personObj2); 

    PersonList personList = new PersonList(); 
    personList.setPersonList(personArrayList); 

    Gson gson = new GsonBuilder().setPrettyPrinting().create(); 

    String json = gson.toJson(personList); 

    try { 
     // write converted json data to a file named "PersonList.json" 
     FileWriter writer = new FileWriter("C:\\Users\\Dylan\\JsonFiles\\PersonList.json"); 
     writer.write(json); 
     writer.close(); 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    try 

    { 


     SSLContextBuilder builder = new SSLContextBuilder(); 
     builder.loadTrustMaterial(null, new TrustSelfSignedStrategy()); 
     SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build()); 
     CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); 

     HttpPost postRequest = new HttpPost(
       "https://***************.brightpattern.com:####/admin/ws/t/**************.brightpattern.com/callinglist/add/ListOldLeads"); 

     UsernamePasswordCredentials creds = 
        new UsernamePasswordCredentials("username", "password"); 
     postRequest.addHeader(new BasicScheme().authenticate(creds, postRequest, null)); 

     StringEntity input = new StringEntity(json); 
     input.setContentType("application/json"); 
     postRequest.setEntity(input); 

     HttpResponse response = httpClient.execute(postRequest); 

     BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent()))); 

     String output; 
     System.out.println("Output from Server .... \n"); 
     while ((output = br.readLine()) != null) { 
      System.out.println(output); 
     } 

    } catch (MalformedURLException e) { 

     e.printStackTrace(); 

    } catch (IOException e1) { 

     e1.printStackTrace(); 

    } 
    catch (KeyStoreException e2) { 

     e2.printStackTrace(); 

    } 
    catch (NoSuchAlgorithmException e3) { 

     e3.printStackTrace(); 

    } 
    catch (KeyManagementException e4) { 

     e4.printStackTrace(); 

    } 
    catch (AuthenticationException e5) { 

     e5.printStackTrace(); 

    } 
} 
} 

此外,這裏的文件,我應該如何提交這個。如果任何人都能發現我的問題會是如此之大。提前致謝。

http://www.brightpattern.com/doc/Documentation/ServicePattern/3.10/list-management-api-specification/Addarecord

回答

0

我想通了,這就是我需要的

CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); 
     credentialsProvider.setCredentials(AuthScope.ANY, 
      new UsernamePasswordCredentials("username", "password")); 
     CloseableHttpClient httpClient = 
      HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).build();