2016-05-17 78 views
1

我試圖連接到我們的SharePoint和POST一些數據到列表。從Java發佈到SharePoint 2013

用戶可以與Web-App進行交互併發送一些信息。這些數據將被髮送到運行在tomcat上的Java-Web-Interface。 Java代碼應連接到我們的SharePoint並將數據發佈到列表中。今天,我在網上閱讀了很多教程和資源......他們中的大多數人都棄用礦石討論輕微不同的情況!所以!我的腦海裏低聲說道:「繼續,訪問stackoverflow。」在這裏,我問了這個問題:

情況如上所述。我打電話給一個web界面vie JS(angularJS)並傳遞一個用戶在前端輸入的電子郵件地址。這裏有雲在:

@Path("webservice") 
public class SetEmail { 
    @POST 
    @Path("/SetEmail") 
    @Consumes(MediaType.APPLICATION_JSON + ";charset=UTF-8") 
    @Produces("text/plain") 
    public String addItem(String incoming) throws ClientProtocolException, IOException, AuthenticationException{ 

     String result = "error"; 
     JSONObject jsonObj = new JSONObject(incoming); 
     String listName = "Leads"; 
     String username = "..."; 
     char[] password= new char[]{'...', '...', ...}; 
     String website = "..."; 

現在,畢竟我讀,我得從SharePoint DigestValue中,因爲我想打一個POST請求:

 //Get the Digestvalue. 
     CredentialsProvider provider = new BasicCredentialsProvider(); 
     provider.setCredentials(AuthScope.ANY, new NTCredentials(username, password.toString(), "http://...", "https://...")); 
     HttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build(); 

     HttpPost httpPost = new HttpPost(website + "_api/contextinfo"); 
     httpPost.addHeader("Accept", "application/json;odata=verbose"); 
     httpPost.addHeader("content-type", "application/json;odata=verbose"); 
     httpPost.addHeader("X-ClientService-ClientTag", "SDK-JAVA"); 
     HttpResponse response = client.execute(httpPost); 

     byte[] content = EntityUtils.toByteArray(response.getEntity()); 
     String jsonString = new String(content, "UTF-8"); 
     System.out.println(response); 
     JSONObject json = new JSONObject(jsonString); 
     String FormDigestValue = json.getJSONObject("d").getJSONObject("GetContextWebInformation").getString("FormDigestValue"); 

得到消化後,我能夠執行實際的請求:

 //POST the data. 
     CloseableHttpClient client2 = HttpClients.createDefault(); 
     HttpPost httpPost2 = new HttpPost(website + "_api/web/lists/GetByTitle(" + listName + ")"); 

     httpPost2.setEntity(new StringEntity("test post")); 
     NTCredentials creds = new NTCredentials(username, password.toString(), "http://...", "https://..."); 
     httpPost2.addHeader(new BasicScheme().authenticate(creds, httpPost2, null)); 
     httpPost2.addHeader("X-RequestDigest", FormDigestValue); 
     httpPost2.addHeader("Accept", "application/json;odata=verbose"); 
     httpPost2.addHeader("Content-Type", "application/json;odata=verbose"); 

     CloseableHttpResponse response2 = client2.execute(httpPost2); 
     System.out.println(response2); 
     client2.close(); 
    } 
} 

我知道這不是最美的代碼,是的,我不是Java專家。我的問題是:

  1. 我不知道天氣所有這些代碼片段的都是最新或 天氣我使用過時的。也許有人能夠 賜教。
  2. 我使用Apache的HttpClient。對我來說,它看起來像最可用的庫。是對的嗎?
  3. 每次我在前端執行操作,我的代碼開始 正在運行,我得到一個HTTP 401未經授權的錯誤。我試過 各種代碼,但都沒有效果。

    HttpResponseProxy{HTTP/1.1 401 Unauthorized [Server: Microsoft-IIS/8.0, SPR.. 
    

也許有人有耐心地告訴我該怎麼做。謝謝。

回答

0

哇......你真的在這裏嘗試一些黑魔法;) - 我建議你在一個像Postman或其他REST工具工作的工具中獲取HTTP POST/GET,然後返回到你的代碼。

我不知道你想實現什麼,但它可能會更容易通過PowerShell中去(如果你想創建一個遷移腳本)或JavaScript(如果你是一個網站上)。

注意驗證在SharePoint不同Online和SharePoint的前提......這也是由貴公司定製的(例如,您可以實現基於表單的身份驗證以及)。一定要知道你的SharePoint使用什麼。 (或股本一些更多的信息,以便我們能夠幫助)

+0

Thougt這種解決方案可能會有點「註銷的方式」。 ;)但是可以在互聯網上找到關於這個主題的很多教程。我會重新思考,也許會嘗試另一種解決方案。 – Nico

+0

嘿尼科,我發現這個線程,也許它可以幫助你以正確的方式:http://sharepoint.stackexchange.com/questions/79803/how-to-authenticate-user-in-sharepoint-online-using-javascript – Nils

+0

謝謝。它着重解決另一個問題,但可能是一個解決方案。 :) – Nico