我正在尝试从Json
API以SharePoint
的形式下载一些数据。我能够使用WGET
来完成它,我正在尝试用Java程序做同样的事情。以下是我的代码:
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST,AuthScope.ANY_PORT),
new NTCredentials("username","password",null,"sharepoint2.domain.com"));
HttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider).build();
try {
HttpGet jsonGet = new HttpGet("URL");
jsonGet.setHeader("Accept","Application/json;odata=verbose");
HttpResponse json = httpClient.execute(jsonGet);
System.out.println(json.toString());
System.out.println(json.getStatusLine());
System.out.println(json.getEntity().toString());
} catch (Exception e){
e.printStackTrace();
}
当我运行上面的代码时,我收到以下错误:
WARNING: NEGOTIATE authentication error: No valid credentials provided (Mechanism level: No valid credentials provided (Mechanism level: Failed to find any Kerberos tgt))
HttpResponseProxy{HTTP/1.1 401 Unauthorized [Server: Microsoft-IIS/8.5, SPRequestGuid: 8dd6489e-f91e-1018-e731-4711c8a5147c, request-id: 8dd6489e-f91e-1018-e731-4711c8a5147c, SPRequestDuration: 23, SPIisLatency: 0, WWW-Authenticate: NTLM, WWW-Authenticate: Negotiate, X-Powered-By: ASP.NET, MicrosoftSharePointTeamServices: 15.0.0.4859, X-Content-Type-Options: nosniff, X-MS-InvokeApp: 1; RequireReadOnly, Date: Fri, 09 Feb 2018 15:43:03 GMT, Content-Length: 0]
我对kerberos
的了解有限,并且不理解错误的含义。我使用与WGET
相同的kerberos票证运行java程序。它适用于WGET
,但不确定为什么它会在Java程序中引发错误。
这是我的WGET命令的输出:
$wget --user username --password password --header='Accept: application/json;odata=verbose' "URL"
--2018-02-09 10:20:55-- 'URL'
Resolving sharepoint2.domain.com... xxx.xxx.xxx.xxx
Connecting to sharepoint2.domain.com|xxx.xxx.xxx.xxx|:80... connected.
HTTP request sent, awaiting response... 401 Unauthorized
Reusing existing connection to sharepoint2.domain.com:80.
HTTP request sent, awaiting response... 401 Unauthorized
Reusing existing connection to sharepoint2.domain.com:80.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [application/json]
Saving to: “items?$top=1000.7”
从输出中,我看到它尝试授权并失败两次,然后使用现有连接。我不确定为什么会这样。
那么,我如何让我的java程序与wget
做同样的事情呢?任何帮助将不胜感激,谢谢。
更新
我尝试像这样设置krb5.conf
和jass.conf
:
System.setProperty("java.security.krb5.conf","path");
System.setProperty("java.security.auth.login.config","path");
但是,它仍然给我同样的错误。