代码之家  ›  专栏  ›  技术社区  ›  Jesse Lynn

SharePoint REST API with Java-身份验证错误

  •  0
  • Jesse Lynn  · 技术社区  · 6 年前

    我有以下Java代码向SharePoint REST API发送POST请求以创建列表,它返回以下身份验证错误:

        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) {
        }
    

    控制台输出

    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."}}}
    

    我可以使用非常类似的代码向REST API发送GET请求,以检索所有列表、检索列表项、执行所有这些读取操作。但是,这不适用于POST请求。我做错什么了吗?提供的凭据适用于对整个网站集具有完全控制权的帐户,因此我们可以排除权限错误。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Jesse Lynn    6 年前

    好的,问题很简单。该行:

        String formDigestValue = j.get("d").get("GetContextWebInformation").get("FormDigestValue").toString();          
    

    返回带引号的formDigestValue。使用asText()而不是toString()会有所帮助。