繁体   English   中英

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

[英]HowTrying to consume Rest api of Sharepoint from java client but getting status code 403 Forbidden

我试图通过Java客户端使用Sharepoint的rest api获取文件,但得到403 Forbidden错误代码。

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

我在标题中发送授权仍然遇到相同的错误。 用肥皂水wsdl也尝试过同样的事情,但是得到了同样的反应。

这就是我使用NTML进行身份验证的方式-诀窍是从此示例更改为NTCredentials()与UsernamePasswordCredentials()-> https://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache /http/examples/client/ClientAuthentication.java

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM