如何尝试从java客户端使用Sharepoint的Rest api,但禁止获取状态代码403
我试图通过java客户端使用Sharepoint的RESTAPI获取文件,但得到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";
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();
}
}
}