从JAVA到Sharepoint 2013 REST API的BASIC身份验证 [英] BASIC authentication from JAVA to Sharepoint 2013 REST API

查看:847
本文介绍了从JAVA到Sharepoint 2013 REST API的BASIC身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Java application needs access to SharePoint 2013 REST API https://msdn.microsoft.com/en-us/library/office/jj860569.aspx

更愿意使用BASIC身份验证:

Would prefer to use BASIC authentication:

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

There are many examples of using the rest api's on the web but none seem to deal with authentication. Maybe I'm missing something really simple here.

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

This works manually via POSTMAN: http://tech.bool.se/basic-rest-request-sharepoint-using-postman/ but requires me to enter username and password in browser.

我尝试过这样做:
HttpClientBuilder基本身份验证
使用

I've tried implementing this: HttpClientBuilder basic auth using

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

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

This results in -> WARNING: NTLM authentication error: Credentials cannot be used for NTLM authentication: org.apache.http.auth.UsernamePasswordCredentials

推荐答案

感谢@fateddy提供的技巧:
请记得切换出UsernamePasswordCredentials( username,password));对于NTCredentials(,,,);

Thanks @fateddy that does the trick: Remember to switch out UsernamePasswordCredentials("username", "password"));for NTCredentials(, , ,);

使用此 maven依赖

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

对SharePoint的身份验证有效:

The authentication to SharePoint works:

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

And you end up with : HTTP/1.1 200 OK

这篇关于从JAVA到Sharepoint 2013 REST API的BASIC身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆