Post to SharePoint 2013 from Java

2019-05-31 00:32发布

问题:

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

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:

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 = "...";

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();
    }
}

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

  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.

回答1:

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.

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).

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)