从 JAVA 到 Sharepoint 2013 REST API 的基本身份验证

BASIC authentication from JAVA to Sharepoint 2013 REST API

Java 应用程序需要访问 SharePoint 2013 REST API https://msdn.microsoft.com/en-us/library/office/jj860569.aspx

更愿意使用 BASIC 身份验证:

网络上有很多使用其余 api 的示例,但 none 似乎处理身份验证。也许我在这里遗漏了一些非常简单的东西。

这通过 POSTMAN 手动工作: http://tech.bool.se/basic-rest-request-sharepoint-using-postman/ 但要求我在浏览器中输入用户名和密码。

我试过实现这个: 使用

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.4.1</version>
</dependency>

这导致 -> 警告:NTLM 身份验证错误:凭据不能用于 NTLM 身份验证:org.apache.http.auth.UsernamePasswordCredentials

感谢@fateddy 成功了: 记得换掉 UsernamePasswordCredentials("username", "password"));for NTCredentials(, , ,);

使用这个 maven dependency:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.4.1</version>
</dependency>

对 SharePoint 的身份验证有效:

import org.apache.http.client.CredentialsProvider;
import org.apache.http.auth.AuthScope;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.auth.NTCredentials;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;

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

你最终得到: HTTP/1.1 200 行