gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:44:51 24 4
gpt4 key购买 nike

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

更愿意使用 BASIC 身份验证:

网络上有很多使用其余 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

最佳答案

感谢@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 正常

关于java - 从 JAVA 到 Sharepoint 2013 REST API 的基本身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29570383/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com