从Java发布到SharePoint 2013

我尝试连接到我们的SharePoint并将一些数据发布到列表中。

用户可以与Web应用程序交互并发送一些信息。 这些数据将发送到在tomcat上运行的Java-Web-Interface。 Java代码应该连接到我们的SharePoint并在列表中发布数据。 今天,我在网上阅读了很多教程和资源……他们中的大多数都被弃用,讨论的情况略有不同! 所以! 我的脑子低声说:“继续访问stackoverflow。” 在这里,我问这个问题:

情况如上所述。 我称之为web-Interface 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. 每次我在前端执行Action并且我的代码开始运行时,我收到HTTP 401 Unauthorized错误。 我尝试了各种各样的代码但没有一个很好用。

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

也许有人有耐心告诉我该怎么做。 谢谢。

哇…你真的在这里尝试一些黑魔法;) – 我建议你在Postman或其他一些REST工具工作中使用HTTP POST / GET,然后返回你的代码。

我不确切地知道你想要实现什么,但通过powershell(如果你试图创建一个迁移脚本)或JavaScript(如果你在网站上)可能更容易。

请注意,SharePoint在线和SharePoint内部部署的身份validation不同……您的公司也可以自定义(例如,您也可以实现基于表单的身份validation)。 请务必了解您的SharePoint正在使用的内容。 (或分享更多信息,以便我们提供帮助)