JAVA实现向SharePoint 2013 REST API进行BASIC身份验证

12

Java应用需要访问SharePoint 2013 REST API:https://msdn.microsoft.com/zh-cn/library/office/jj860569.aspx

想要使用基本身份验证:

网络上有很多使用REST API的示例,但好像没有涉及到身份验证。也许我在这里漏掉了什么非常简单的东西。

这个手动用POSTMAN是可行的: http://tech.bool.se/basic-rest-request-sharepoint-using-postman/ 但需要我在浏览器中输入用户名和密码。

我尝试着实现以下内容: HttpClientBuilder basic auth 使用

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

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


1
看起来你需要NTLM身份验证 -> https://hc.apache.org/httpcomponents-client-ga/ntlm.html - fateddy
@fateddy 谢谢你提供的链接,解决了问题。 - Eric Nord
1个回答

10

感谢 @fateddy,这就解决了问题: 记得将 UsernamePasswordCredentials("username", "password") 替换为 NTCredentials(, , ,);

使用这个 maven 依赖

<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 OK


1
我尝试了您提供的解决方案,用于验证我的Java服务与SP2013搜索端点的身份,但我仍然收到以下错误消息: “SafeQueryPropertiesTemplateUrl“{0}”不是有效的URL。” - Jonathan Anctil
1
是的,它终于可以工作了。我使用WinHttpClients.createDefault()。但安装过程中也出现了一些问题,我不知道IT人员都做了什么! - Jonathan Anctil
1
谢谢。我使用了你的解决方案,它起作用了。但是我在共享点上登录站点时遇到了问题。过了一段时间后,我尝试检查IIS日志文件,并发现我使用的域名是错误的。现在一切正常! - Dharshni
4
“hostname”和“domain”字段应该填什么? - Franjo Pintarić
2
我需要访问由公司SAML认证的SharePoint。我需要连接到URL https://xyz.sharepoint.com/teams/x/y/z/AllItems.aspx 并从该页面下载文件。您能否建议我如何使用Java RESTful客户端应用程序完成此操作?我已经使用上述代码进行连接,但是收到了HTTP/1.1 403 Forbidden的错误提示。 - Radhakrishna
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接