HowTrying从Java客户端消耗的SharePoint REST API,但得到故宫状态码40

2019-10-22 09:19发布

我试图打通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还,但得到同样的答复尝试同样的事情。

Answer 1:

我如何与NTML验证这-招是改变从这个例子NTCredentials()VS 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();
    }
}
}


文章来源: HowTrying to consume Rest api of Sharepoint from java client but getting status code 403 Forbidden