kikovalle / PLGSharepointRestAPI-java

Easy to use wrapper for the Sharepoint Rest API v1. Even if this is not a full implementation it covers most common use cases and provides examples to extending this API.
MIT License
42 stars 32 forks source link

reqstatus=0x8004882c errorstatus=0x80045b00 when authenticating #27

Closed cpesch closed 3 years ago

cpesch commented 3 years ago

Hi,

I've got the typical simple cmd line client for accessing the company Sharepoint which is secured by the company AD:

import org.json.JSONObject;
import com.panxoloto.sharepoint.rest.PLGSharepointClient;
import com.panxoloto.sharepoint.rest.PLGSharepointClientOnline;

public class SharepointDownloader {
    public static void main(String[] args) throws Exception {
        String user = "domain\\username";
        String passwd = "password";
        String domain = "company.sharepoint.com";
        String spSiteUrl = "/sites/asite";

        PLGSharepointClient wrapper = new PLGSharepointClientOnline(user, passwd, domain, spSiteUrl);
        JSONObject result = wrapper.getAllLists("{}");
        System.out.println(result);
    }
}

and I always get the above error codes

`<?xml version="1.0" encoding="utf-8"?> <S:Envelope xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wst="http://schemas.xmlsoap.org/ws/2005/02/trust" xmlns:S="http://www.w3.org/2003/05/soap-envelope">

http://schemas.xmlsoap.org/ws/2005/02/trust/RSTR/Issue http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous 2021-08-20T13:24:48.331086Z 2021-08-20T13:29:48.331086Z company.sharepoint.com 0x8004882c 0x80045b00 ` All I found was this Stackoverflow article https://stackoverflow.com/questions/59320628/unable-to-get-access-token-from-sharepoint Is there a problem when accessing with AD accounts?
kikovalle commented 3 years ago

Have you tried with the OnPremises implementation? In the code example you provided you are instantiating a Online implementation (that should work with an online office365 sharepoint instance). If you are using a company OnPremises . Test with this and let me know if it works for you.

cpesch commented 3 years ago

Actually, I assumed that *.sharepoint.com are Online implementations. Anyway, I've tried the OnPremises client and always receive a 401.

Could it be that I've interpreted domain, spSiteUrl and spSitePrefix incorrectly? They seem to have a different meaning than for the Online client

I assume domain is the AD-domain, i.e. everything before the two backslashes. in uppercase characters? And spSiteUrl is the FQDN?

public class SharepointDownloaderOnPremise {
    public static void main(String[] args) throws Exception {
        String user = "username";
        String passwd = "password";
        String domain = "AD-DOMAIN";
        String spSiteUrl = "company.sharepoint.com";
        String spSitePrefix = "/sites/asite";

        PLGSharepointClient wrapper = new PLGSharepointOnPremisesClient(user, passwd, domain, spSiteUrl, spSitePrefix);
        JSONObject result = wrapper.getAllLists("{}");
        System.out.println(result);
    }
}
kikovalle commented 3 years ago

The online implementation tries to login with the microsoft SSO obtaining a token valid for online instances. Using an OnPremises sharepoint the auth is done by creating NTCredentials to add as a header

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY, new NTCredentials(user, passwd, spSiteUrl, domain));
    CloseableHttpClient httpClient = HttpClients.custom()
            .setDefaultCredentialsProvider(credsProvider)
            .build();

So in this case the meaning are related to this constructor org.apache.http.auth.NTCredentials.NTCredentials(String userName, String password, String workstation, String domain)

So userName.- an AD user name (without domain) passwd.- the user password to login against your AD spSiteUrl.- the workstation name of the sharepoint on premises instance domain.- the user domain, in AD the domain containing the user used to login.

Let me know if this helps, if not i can provide a more flexible implementation where you can provide a token generated with other tools to access your sp instance.

P.D.- sorry for the time in answering but i have busy days at work.

cpesch commented 3 years ago

@kikovalle the error was related to a missing app registration and authorization

kikovalle commented 3 years ago

@kikovalle the error was related to a missing app registration and authorization

Thanks for the info as i was trying to get some time to try to reproduce the issue and fix it, but unfortunately i am a bit busy at work.