Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何尝试从java客户端使用Sharepoint的Rest api,但禁止获取状态代码403_Java_Rest_Sharepoint_Soap - Fatal编程技术网

如何尝试从java客户端使用Sharepoint的Rest api,但禁止获取状态代码403

如何尝试从java客户端使用Sharepoint的Rest api,但禁止获取状态代码403,java,rest,sharepoint,soap,Java,Rest,Sharepoint,Soap,我试图通过java客户端使用Sharepoint的RESTAPI获取文件,但得到403禁止的错误代码 Client c = Client.create(); WebResource resource = c.resource("http://URL/_api/web/GetFolderByServerRelativeUrl('/Folder')/Files"); String userCredentials = "Username:Password";

我试图通过java客户端使用Sharepoint的RESTAPI获取文件,但得到403禁止的错误代码

    Client c = Client.create();
    WebResource resource = c.resource("http://URL/_api/web/GetFolderByServerRelativeUrl('/Folder')/Files");     
    String userCredentials = "Username:Password";
    resource.header("Authorization", "Basic " + new String(new Base64().encode(userCredentials.getBytes())));
    resource.header("Accept","application/json; odata=verbose");
    String response = resource.get(String.class);

我正在发送头中的授权,但仍然面临相同的错误。使用soap wsdl也尝试了同样的方法,但得到了相同的响应。

这就是我使用NTML进行身份验证的方式-技巧是从本例中更改为NTCredentials vs UsernamePasswordCredentials->

Maven依赖项: org.apache.httpcomponents httpclient 4.4.1

public class SharePointClientAuthentication {

public static void main(String[] args) throws Exception {
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(
        new AuthScope(AuthScope.ANY),
        new NTCredentials("username", "password", "https://hostname", "domain"));
    CloseableHttpClient httpclient = HttpClients.custom()
        .setDefaultCredentialsProvider(credsProvider)
        .build();
    try {
        HttpGet httpget = new HttpGet("http://hostname/_api/web/lists");

        System.out.println("Executing request " + httpget.getRequestLine());
        CloseableHttpResponse response = httpclient.execute(httpget);
        try {
            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            EntityUtils.consume(response.getEntity());
       } finally {
        response.close();
    }
    } finally {
        httpclient.close();
    }
}
}