无法使用所有Python插件的用户名和密码对SharePoint API进行身份验证

问题描述 投票:2回答:1

我的用例是从公司的sharepoint(在线)站点获取一些文件。我的用户名密码已被授予读取访问权限,以通过SharePoint API连接。对于通话,我将必须通过代理和公司SSL验证。

我尝试使用许多API,例如sharepyOffice365-REST-Python-ClientHttpNtlmAuth,HTTPBasicAuth,但所有这些给了我[SSL:CERTIFICATE_VERIFY_FAILED]错误。

我不确定是否可以将证书传递给这些API。

还有其他我可以尝试使用的插件吗?

ssl-certificate python-3.7 sharepoint-online sharepoint-api
1个回答
0
投票

对于此plugin,作为一种解决方法,我对将请求发送到API的常用功能做了猴子补丁。以下是一些此类功能的示例:

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()
© www.soinside.com 2019 - 2024. All rights reserved.