简体   繁体   中英

Access SharePoint Rest api via Java Client but get 403 error

I'm trying to access SP Rest API as mentioned here , but still get 403 - Forbidden error. I'm not sure if it has something to do with the single sign-on we use: To access the Rest Api via browser I have to login into https://login.microsoftonline.com and then I am able to call https://mycompany.sharepoint.com/_api/search/query?querytext=%27Search%20foo%20bar%27 So my Java App looks like this:

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(
            new AuthScope(AuthScope.ANY),
            new NTCredentials("username@mycompany.de", "password", "https://login.microsoftonline.com", "microsoftonline.com"));
    CloseableHttpClient httpclient = HttpClients.custom()
            .setDefaultCredentialsProvider(credsProvider)
            .build();
    try {
        HttpGet httpget = new HttpGet("https://mycompany.sharepoint.com/_api/search/query?querytext=%27Search%20foo%20bar%27");

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

Which leads to HTTP/1.1 403 FORBIDDEN . Any ideas?

EDIT: I added a print.out of all headers, so my full output is:

Executing request GET https://***.sharepoint.com/_api/search/query?querytext=%27Search%20foo%20OR%20bar%27 HTTP/1.1
log4j:WARN No appenders could be found for logger (org.apache.http.client.protocol.RequestAddCookies).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
----------------------------------------
HTTP/1.1 403 Forbidden
Cache-Control: private, max-age=0
Transfer-Encoding: chunked
Content-Type: application/xml;charset=utf-8
Expires: Mon, 15 Jun 2015 07:12:48 GMT
Last-Modified: Tue, 30 Jun 2015 07:12:48 GMT
Server: Microsoft-IIS/8.5
X-SharePointHealthScore: 0
X-Forms_Based_Auth_Required: https://***.sharepoint.com/_forms/default.aspx?ReturnUrl=/_layouts/15/error.aspx&Source=%2f_vti_bin%2fclient.svc%2fsearch%2fquery%3fquerytext%3d%2527Search%2520foo%2520OR%2520bar%2527
X-Forms_Based_Auth_Return_Url: https://***.sharepoint.com/_layouts/15/error.aspx
X-MSDAVEXT_Error: 917656; Zugriff+verweigert.+Zum+%c3%96ffnen+von+Dateien+an+diesem+Speicherort+m%c3%bcssen+Sie+zun%c3%a4chst+zur+Website+wechseln+und+die+Option+zur+automatischen+Anmeldung+aktivieren.
DATASERVICEVERSION: 3.0
X-AspNet-Version: 4.0.30319
X-IDCRL_AUTH_PARAMS_V1: IDCRL Type="BPOSIDCRL", EndPoint="/_vti_bin/idcrl.svc/", RootDomain="sharepoint.com", Policy="MBI"
SPRequestGuid: c757159d-a063-2000-20ed-499660c844ff
request-id: c757159d-a063-2000-20ed-499660c844ff
X-FRAME-OPTIONS: SAMEORIGIN
X-Powered-By: ASP.NET
MicrosoftSharePointTeamServices: 16.0.0.4121
X-Content-Type-Options: nosniff
X-MS-InvokeApp: 1; RequireReadOnly
P3P: CP="ALL IND DSP COR ADM CONo CUR CUSo IVAo IVDo PSA PSD TAI TELo OUR SAMo CNT COM INT NAV ONL PHY PRE PUR UNI"
Date: Tue, 30 Jun 2015 07:12:47 GMT

I try to translate the X-MSDAVEXT_Error to english: Permission denied. To access data from this location you have to switch to the website and enable the option for automatic login. Permission denied. To access data from this location you have to switch to the website and enable the option for automatic login.

It looks like you're using Office 365. I've seen this issue when attempting to use some accounts. I'd take a look at the ADAL libraries for performing authentication against Azure AD (Which you have as part of O365) https://github.com/AzureAD/azure-activedirectory-library-for-java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM