我试图连接到我们的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专家。我的问题是:
- 我不知道天气所有这些代码片段的都是最新或 天气我使用过时的。也许有人能够 赐教。
- 我使用Apache的HttpClient。对我来说,它看起来像最可用的库。是对的吗?
每次我在前端执行操作,我的代码开始 正在运行,我得到一个HTTP 401未经授权的错误。我试过 各种代码,但都没有效果。
HttpResponseProxy{HTTP/1.1 401 Unauthorized [Server: Microsoft-IIS/8.0, SPR..
也许有人有耐心地告诉我该怎么做。谢谢。
Thougt这种解决方案可能会有点“注销的方式”。 ;)但是可以在互联网上找到关于这个主题的很多教程。我会重新思考,也许会尝试另一种解决方案。 – Nico
嘿尼科,我发现这个线程,也许它可以帮助你以正确的方式:http://sharepoint.stackexchange.com/questions/79803/how-to-authenticate-user-in-sharepoint-online-using-javascript – Nils
谢谢。它着重解决另一个问题,但可能是一个解决方案。 :) – Nico