4

My use case is to get some files from company's sharepoint (Online) site. I have been granted read access for my username password to connect through SharePoint API. For the calls I will have to pass proxy and company SSL verification.

I have tried using a number of APIs such as sharepy, Office365-REST-Python-Client, HttpNtlmAuth, HTTPBasicAuth, but all of them giving me [SSL: CERTIFICATE_VERIFY_FAILED] error.

I am not sure if passing certificate to these APIs is possible or not.

Is there any other plugin that I can try for my scenario?

1 Answers1

0

For this plugin, as a work-around I have done monkey patching for the common functions that send requests to the APIs. Following are the examples of few such functions:

class SharePointApi:
"""SharePoint aceess api."""
def __init__(self):
    self.base_url = configReader.get('SHAREPOINT', 'URL')
    self.ctx_auth = AuthenticationContext(self.base_url)
    self.ctx_auth.provider = SamlTokenProvider(self.base_url, username, password)
    self.ctx_auth.provider.acquire_service_token = self._patched_acquire_service_token
    self.ctx_auth.provider.acquire_authentication_cookie = self._patched_acquire_authentication_cookie
    self.ctx_auth.provider.get_realm_from_target_url = self._patched_get_realm_from_target_url
    self.ctx_auth.provider.get_app_only_access_token = self._patched_get_app_only_access_token


def _patched_acquire_authentication_cookie(self, options):
    """Retrieve SPO auth cookie"""
    url = options['endpoint']
    session = requests.session()
    session.post(url, data=self.ctx_auth.provider.token, headers={'Content-Type': 'application/x-www-form-urlencoded'}
        , verify=False
    )
    logger.debug_secrets("session.cookies: %s", session.cookies)
    cookies = requests.utils.dict_from_cookiejar(session.cookies)
    logger.debug_secrets("cookies: %s", cookies)
    if 'FedAuth' in cookies and 'rtFa' in cookies:
        self.ctx_auth.provider.FedAuth = cookies['FedAuth']
        self.ctx_auth.provider.rtFa = cookies['rtFa']
        return True
    self.ctx_auth.provider.error = "An error occurred while retrieving auth cookies"
    logger.error(self.ctx_auth.provider.error)
    return False

def _patched_get_realm_from_target_url(self):
    response = requests.head(url=self.ctx_auth.provider.url, headers={'Authorization': 'Bearer'}, verify=False, proxies=proxies)
    return self.ctx_auth.provider.process_realm_response(response)

def _patched_get_app_only_access_token(self, target_host, target_realm):
    resource = self.ctx_auth.provider.get_formatted_principal(self.ctx_auth.provider.SharePointPrincipal, target_host, target_realm)
    client_id = self.ctx_auth.provider.get_formatted_principal(self.ctx_auth.provider.client_id, None, target_realm)
    sts_url = self.ctx_auth.provider.get_security_token_service_url(target_realm)
    oauth2_request = self.ctx_auth.provider.create_access_token_request(client_id, self.ctx_auth.provider.client_secret, resource)
    response = requests.post(url=sts_url, headers={'Content-Type': 'application/x-www-form-urlencoded'}, data=oauth2_request, verify=False, proxies=proxies)
    return response.json()