如何使用MS SharePoint进行身份验证以将其API与Java一起使用?



我正在努力用MS SharePoint进行身份验证以使用它的API。我一直在谷歌上搜索和解决这个问题一段时间,但不知何故我找不到解决方案。到目前为止,最有希望的解决方案是基于这个答案。

我使用的依赖项是:

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

这是我的代码:

public static void callRestEasyService() throws Exception {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(AuthScope.ANY),
new NTCredentials("user", "password", "https://workstation.de", "domain.de"));
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.build();
try {
HttpGet httpget = new HttpGet("https://adress/_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();
}
}

我认为代码是不言自明的,它基本上只是链接答案所暗示的。我的调查表明,解决这个问题的最佳方法是NTCredentials。我还尝试了其他一些替代方案,例如基本身份验证,但在所有情况下我都会收到:

HTTP/1.1 401 Unauthorized

我还尝试使用Samba JCIFS作为替代NTLM引擎。

此外,我有点害怕工作站(第三个参数(或域的参数可能被我错误地填写了。文档说:

工作站 - 发起身份验证请求的工作站。实质上,此计算机的计算机名称。

所以我尝试填写我的机器名称,但网络上的许多示例都建议您使用尝试进行身份验证的 URL。这给我带来了一些困惑,但是由于这两个选项中没有一个,我无法让它工作。

有谁知道为什么是或有该问题的可能解决方案或解决方法?SharePoint 是否有可能通过编程的客户端限制访问?据我所知,至少不可能从SharePoint禁用API。对此有什么想法/想法吗?

我没有设法让它与 apache http 客户端一起使用,而不是这个,我尝试从我的 java 代码运行 cURL 请求以接近 SharePoint API,这工作正常。如果其他人有类似的问题并想尝试解决方法,这是我运行 cURL 请求的源代码:

ProcessBuilder pb = new ProcessBuilder(
"curl",
"https://mysharepoint/_api/web/lists/getbytitle('listName')/items",
"-v",
"--ntlm",
"--negotiate",
"-u",
"user:password"
);
Process p = pb.start();
InputStream is = p.getInputStream();
return createHashMapForStaff(convertStreamToString(is));

返回行(createHashMapForStaff(convertStreamToString(is((只是根据我的需求调整从SharePoint检索到的.XML。来自进程 p 的输入流基本上是你需要的。

最新更新