title: SharePoint REST API with Java - Authentication error
body: <p>I have the following Java code to send a POST request to SharePoint REST API to create a list and it returns the following authentication errors:</p>
  
  <pre><code>    CloseableHttpClient httpClient = null;
      try {
  
          String user = xxx;
          String password = xxx;
          String domain = xxx;
          String workstation = "";
          CredentialsProvider credsProvider = new BasicCredentialsProvider();
          credsProvider.setCredentials(new AuthScope(AuthScope.ANY),
                  new NTCredentials(user, password, workstation, domain));
          httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
  
  
  
          String digestQueryURL = "http://my_sharepoint_site/_api/contextinfo";
          HttpPost httpPost = new HttpPost(digestQueryURL);
          httpPost.addHeader("Accept", "application/json;odata=verbose");
          CloseableHttpResponse response = httpClient.execute(httpPost);
          byte[] content = EntityUtils.toByteArray(response.getEntity());
  
          String jsonString = new String(content, "UTF-8");           
          ObjectMapper mapper = new ObjectMapper();
          JsonNode j = mapper.readTree(jsonString);
          String formDigestValue = j.get("d").get("GetContextWebInformation").get("FormDigestValue").toString();          
          response.close();
  
  
  
          // now try to create the list
          String url = "http://my_sharepoint_site/_api/web/lists";
          HttpPost httpPost2 = new HttpPost(url);
          httpPost2.addHeader("X-RequestDigest", getFormDigest(httpClient));
          httpPost2.addHeader("Accept", "application/json;odata=verbose");
          httpPost2.addHeader("Content-Type", "application/json;odata=verbose");
  
          String body = "{ '__metadata': { 'type': 'SP.List' }, 'AllowContentTypes': true, 'BaseTemplate': 100, 'ContentTypesEnabled': true, 'Description': 'My list description', 'Title': 'Test' }";
          StringEntity se = new StringEntity(body);
          httpPost2.setEntity(se);
  
          CloseableHttpResponse response2 = httpClient.execute(httpPost2);
  
          StringBuilder result = new StringBuilder();
          System.out.println(response2.getStatusLine().toString());
          BufferedReader br = new BufferedReader(new InputStreamReader(response2.getEntity().getContent()));
          String output;
          while ((output = br.readLine()) != null) {
              result.append(output);
          }
          System.out.println(result.toString());
  
      } catch (Exception e) {
      }
  </code></pre>
  
  <p>Console output</p>
  
  <pre><code>HTTP/1.1 403 FORBIDDEN
  
  {"error":{"code":"-2130575251, System.Runtime.InteropServices.COMException","message":{"lang":"en-US","value":"The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again."}}}
  </code></pre>
  
  <p>I can use very similar code to send GET requests to the REST API to retrieve all lists, retrieve list items, perform all these read operations. However this does not work for POST requests. Am I doing something wrong? The credentials provided are for an account that has full control over the entire site collection, so we can rule out permission errors.</p>