【问题标题】:facing issue with connecting to SharePoint Online from Python due to cert issue由于证书问题,面临从 Python 连接到 SharePoint Online 的问题
【发布时间】:2020-05-14 15:07:51
【问题描述】:

我正在尝试从 SharePoint 获取列表项到 Python,这是我用于 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)

我收到以下错误的问题 “错误:HTTPSConnectionPool(host='login.microsoftonline.com', port=443): url: /GetUserRealm.srf 超过最大重试次数(由 SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败:无法获取本地颁发者证书 (_ssl.c:1076)')))"

我正在尝试设置 ssl-verify=none 以使 poc 移动,关于如何使用 AuthenticationContext 做到这一点的任何想法

谢谢 内特

【问题讨论】:

    标签: python sharepoint ssl-certificate


    【解决方案1】:

    在最新版本中,RequestOptions.verify 属性被公开,允许:

    控制是否验证服务器接受的TLS证书:

    • 布尔值(默认为True
    • 字符串值,表示要使用的 CA 包的路径

    示例

    可以通过底层 HTTP 请求对象禁用证书验证,如下所示:

    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"])
    

    注意

    一旦禁用,urllib3 可能会向InsecureRequestWarning warning 抱怨,这是因为正在向主机发出未经验证的 HTTPS 请求 {tenant}.sharepoint.com。强烈建议添加证书验证。

    安装

    因为它需要最新版本,所以可以从 GitHub 安装(直到它在 PyPI 中发布):

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

    【讨论】:

    • 感谢 Vadim 编写代码并帮助解决问题。这是我得到的错误。 site-packages\urllib3\connectionpool.py:1004:InsecureRequestWarning:正在发出未经验证的 HTTPS 请求。强烈建议添加证书验证。请参阅:urllib3.readthedocs.io/en/latest/… InsecureRequestWarning,Traceback(最近一次调用最后一次):文件“C:\LocalUser\AppData\Roaming\Python\Python37\site-packages\office365\runtime\client_request.py”,第 36 行,在 execute_query 响应中。 raise_for_status()
    • 文件“C:\LocalUser\AppData\Roaming\Python\Python37\site-packages\requests\models.py”,第 940 行,在 raise_for_status 中引发 HTTPError(http_error_msg, response=self) 请求。 exceptions.HTTPError: 401 Client Error: Unauthorized for url: MySharePointSiteURL/_api/Web 在处理上述异常的过程中,又发生了一个异常:
    • 导入 urllib3 urllib3.disable_warnings()
    • 在 urllib3.disable_warnings() 之后,我收到类似的错误文件“C:\,,,,Python\Python37\site-packages\requests\models.py”,第 940 行,在 raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: mysharepointsiteurl/_api/Web
    • 感谢 Vadim,是的,它是 SharePointOnline 站点。我同意,将在github提出请求
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-13
    • 1970-01-01
    • 2014-08-07
    • 1970-01-01
    • 1970-01-01
    • 2013-04-26
    • 2022-01-01
    相关资源
    最近更新 更多