简体   繁体   中英

facing issue with connecting to SharePoint Online from Python due to cert issue

I am trying a get a list items from SharePoint to Python, here is the code I am using for the poc

ctx_auth = AuthenticationContext(url='https://SharePointSiteURL')
if ctx_auth.acquire_token_for_user(username='MyUser@Company.onmicrosoft.com',password='MyPassword'):
ctx = ClientContext('https://SharePointSiteURL', ctx_auth)
lists = ctx.web.lists
ctx.load(lists)

The issue I am getting following error "Error: HTTPSConnectionPool(host='login.microsoftonline.com', port=443): Max retries exceeded with url: /GetUserRealm.srf (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1076)')))"

I am trying to set the ssl-verify=none to get the poc moving, Any ideas on how I can do that with AuthenticationContext

Thank you Nate

In the latest version RequestOptions.verify property is exposed which allows:

to control whether to verify the server's TLS certificate which accepts:

  • boolean value (defaults to True )
  • string value, which represents path to a CA bundle to use

Example

Certificate verification could be disabled via underlying HTTP request object as demonstrated below:

def disable_ssl(request):
    request.verify = False  # Disable certification verification


ctx = ClientContext.connect_with_credentials("https://contoso.sharepoint.com",
                                             UserCredential(username,
                                                            password)

ctx.get_pending_request().beforeExecute += disable_ssl
web = ctx.web
ctx.load(web)
ctx.execute_query()
print(web.properties["Url"])

Note

Once disabled, urllib3 might complain with InsecureRequestWarning warning which is expected given unverified HTTPS request is being made to host {tenant}.sharepoint.com . Adding certificate verification is strongly advised.

Installation

Since it requires the latest version, it could be installed from GitHub (until it gets published in PyPI):

pip install git+https://github.com/vgrem/Office365-REST-Python-Client.git

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