Я пытаюсь получить файлы, используя rest api Sharepoint через java-клиент, но получая 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() 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();
}
}
}