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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29570383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:13:48  来源:igfitidea点击:

BASIC authentication from JAVA to Sharepoint 2013 REST API

javarestsharepoint

提问by Eric Nord

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

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

Would prefer to use BASIC authentication:

更愿意使用 BASIC 身份验证:

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.

网上有很多使用其余 api 的示例,但似乎没有一个涉及身份验证。也许我在这里遗漏了一些非常简单的东西。

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.

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

I've tried implementing this: HttpClientBuilder basic authusing

我试过实现这个: HttpClientBuilder basic authusing

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

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

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

采纳答案by Eric Nord

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

谢谢@fateddy 做到了这一点:记得切换出 UsernamePasswordCredentials("username", "password")); for NTCredentials(, , ,);

Using this maven dependency:

使用此Maven 依赖项

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

The authentication to SharePoint works:

对 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();
    }
}
}

And you end up with : HTTP/1.1 200 OK

你最终得到: HTTP/1.1 200 OK