问题描述
Java应用程序需要访问SharePoint 2013 REST API
Java application needs access to SharePoint 2013 REST APIhttps://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手动工作:
但要求我在浏览器中输入用户名和密码。
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.
我尝试过这样做:
使用
I've tried implementing this:HttpClientBuilder basic authusing
<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(, , ,);
使用此:
<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身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!