首页 > 解决方案 > 如何使用 OAuth2 访问 SharePoint 列表

问题描述

我有以下代码,我相信它适用于大多数共享点站点,但收到以下错误:

异常(“检查用户名/密码和根站点”)

from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext

url = 'https://company.sharepoint.com'
username = 'user123@company.com'
password = 'password'
listname = 'Test List'


ctx_auth = AuthenticationContext(url)
if ctx_auth.acquire_token_for_user(username, password):
   ctx = ClientContext(url, ctx_auth)
   web = ctx.web
   sp_list = ctx.web.lists.get_by_title(listname)
   items = sp_list.get_items()
   ctx.load(items)
   ctx.execute_query()

else:
   print(ctx_auth.get_last_error())

我将如何操作此代码以确保我可以将 SharePoint 列表中的数据提取到 Python 中?

标签: pythonpython-3.xsharepoint

解决方案


我建议你检查Office365-REST-Python-Client

from office365.runtime.auth.authentication_context import AuthenticationContext
    from office365.sharepoint.client_context import ClientContext

    ctx_auth = AuthenticationContext(url)
    if ctx_auth.acquire_token_for_user(username, password):
      ctx = ClientContext(url, ctx_auth)
      web = ctx.web
      sp_list = ctx.web.lists.get_by_title(listname)
      items = sp_list.get_items()
      ctx.load(items)
      ctx.execute_query()
      # to do

    else:
      print ctx_auth.get_last_error()

推荐阅读