从 Java 发布到 SharePoint 2013 [英] Post to SharePoint 2013 from Java

查看:57
本文介绍了从 Java 发布到 SharePoint 2013的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've tried to connect to our SharePoint and POST some data to a list.

用户可以与 Web 应用交互并发送一些信息.这些数据将被发送到运行在 tomcat 上的 Java-Web-Interface.Java 代码应该连接到我们的 SharePoint 并将数据发布到列表中.今天,我在网上看了很多教程和资源……大部分都是弃用的,或者讨论轻微不同的情况!所以!我的脑海里低声说:继续访问stackoverflow."我在这里,问这个问题:

A user can interact with a Web-App and send some Information. These data will be send to a Java-Web-Interface running on a tomcat. The Java-Code should connect to our SharePoint and post the data in the list. Today, I read a lot of tutorials and ressources on the web... Most of them are deprecated ore discuss lightly different situations! SO! My mind whispered: "Go on and visit stackoverflow." And here I am, asking this question:

情况如上所述.我调用一个 web 接口 vie JS (angularJS) 并传递一个用户在前端输入的电子邮件地址.在这里:

The Situation is described above. I call a web-Interface vie JS (angularJS) and pass an E-Mail-Adress which the user enters in the front-end. Here it goes in:

@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 请求:

Now, after all I read, I have to get the DigestValue from SharePoint, because I want to make a POST-Request:

        //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");

获得摘要后,我可以执行实际请求:

After getting the Digest, I am able to execute the actual request:

        //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 专家.我的问题是:

I know this isn't the most beautiful Code and yes, I am not an Java expert. My Problems are:

  1. 我不知道所有这些代码片段是最新的还是最新的我正在使用已弃用的天气.也许有人能够启发我.
  2. 我正在使用 Apache 的 HttpClient.对我来说它看起来是最可用的库.是吗?
  3. 每次我在前端执行 Action 并且我的代码启动时运行时,我收到 HTTP 401 Unauthorized 错误.我试过了各种代码,但没有一个效果很好.

  1. I don't know weather all of these code-Fragments are up to date or weather I am using deprecated ones. Perhaps someone is able to enlighten me.
  2. I am using HttpClient from Apache. To me it looked like the most usable library. Is that right?
  3. Everytime I execute the Action on the front-end and my Code starts running, I am getting an HTTP 401 Unauthorized error. I tried various Kinds of Code but none worked well.

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

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

Perhaps someone has the Patience to tell me how to do it. Thank you.

推荐答案

哇...你真的在这里尝试了一些黑魔法;) - 我建议你在像 Postman 或其他一些正在运行的 REST 工具,然后返回到您的代码.

Whoa... you are really trying some black magic here ;) - I would suggest you to get your HTTP POST / GET in a tool like Postman or some other REST tool working and then return to your code.

我不确切知道您要实现的目标,但通过 powershell(如果您要创建迁移脚本)或 JavaScript(如果您在网站上)可能更容易.

I don't know exactly what you are trying to achieve, but it might be easier to go via powershell (if you are trying to create a migration script) or JavaScript (if you are on a website).

请注意,SharePoint Online 和本地 SharePoint 中的身份验证不同……这也可由您的公司自定义(例如,您也可以实施基于表单的身份验证).请务必了解您的 SharePoint 正在使用什么.(或分享更多信息,以便我们提供帮助)

Be aware that authentication differs in SharePoint online and SharePoint on premise... this is also customizable by your company (you can for example implement forms-based auth as well). Be sure to know what YOUR SharePoint is using. (Or share some more info, so we can help)

这篇关于从 Java 发布到 SharePoint 2013的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆