如何使用 MS SharePoint 进行身份验证以将其 API 与 Java 一起使用?
How to authenticate with MS SharePoint to use its API with 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
我也试过using Samba JCIFS as an alternative NTLM engine.
此外,我有点害怕工作站(第3个参数)或域的参数可能是我填写错误的。文档说:
workstation - 发出身份验证请求的工作站。本质上,这台机器的计算机名称。
所以我尝试填写我的机器名称,但网络上的许多示例建议您使用 URL 来尝试进行身份验证。这让我有点困惑,但是使用两个选项中的 none 我可以让它工作。
有谁知道为什么会这样,或者有可能解决该问题的方法或变通方法吗? 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));
return 行 (createHashMapForStaff(convertStreamToString(is)) 只是调整从 SharePoint 中检索到的 .XML 以满足我的需要。Process p 中的 InputStream 基本上就是您所需要的。
我正在努力通过 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
我也试过using Samba JCIFS as an alternative NTLM engine.
此外,我有点害怕工作站(第3个参数)或域的参数可能是我填写错误的。文档说:
workstation - 发出身份验证请求的工作站。本质上,这台机器的计算机名称。
所以我尝试填写我的机器名称,但网络上的许多示例建议您使用 URL 来尝试进行身份验证。这让我有点困惑,但是使用两个选项中的 none 我可以让它工作。
有谁知道为什么会这样,或者有可能解决该问题的方法或变通方法吗? 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));
return 行 (createHashMapForStaff(convertStreamToString(is)) 只是调整从 SharePoint 中检索到的 .XML 以满足我的需要。Process p 中的 InputStream 基本上就是您所需要的。