无法使用 python 访问共享点列表

Unable to access sharepoint lists using python

我正在使用 python-sharepoint 从我的 sharepoint 帐户访问数据。但我总是收到 urllib2.HTTPError: HTTP Error 403: Forbidden 错误。我正在关注 this github link。这是我的代码:

from sharepoint import SharePointSite, basic_auth_opener
server_url = 'https://myServerUrl.sharepoint.com/'
site_url = server_url + 'sites/test'
username = 'myUsername.onmicrosoft.com'
password = 'myPassword'

opener = basic_auth_opener(server_url, username, password)
site = SharePointSite(site_url, opener)
for sp_list in site.lists:
    print (sp_list)

我还尝试将 urllib2 opener 创建为:

from sharepoint import SharePointSite
from ntlm import HTTPNtlmAuthHandler
import urllib2

password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(None, server_url, username, password)
auth_handler = HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(password_manager)
opener = urllib2.build_opener(auth_handler)

site = SharePointSite(site_url, opener)
for sp_list in site.lists:
    print (sp_list)

但它又给出了同样的错误。任何解决方案将不胜感激。

SharePoint Online 不支持 BasicNTLM 身份验证方法,这使得 python-sharepoint 不是此处的最佳选择.

相反,我建议尝试 Office365-REST-Python-Client,它支持 Office 365/SharePoint Online。

这是获取站点中所有列表并打印其标题的类似示例

ctx_auth = AuthenticationContext(url='https://contoso.sharepoint.com')
if ctx_auth.acquire_token_for_user(username='jdoe@contoso.onmicrosoft.com'],
                                   password='--password--'):

    ctx = ClientContext(settings['url'], ctx_auth)
    lists = ctx.web.lists
    ctx.load(lists)
    ctx.execute_query()
    for l in lists:
        print(l.properties["Title"])