首页 > 解决方案 > 使用 Python 的 Microsoft Sharepoint 身份验证

问题描述

我正在尝试将文档在线上传到 SharePoint。

目标网址:https ://companyURL.sharepoint.com/sites/A/B/Info_documents/C

我的目标是搜索文件夹 C,如果文件夹 C 中存在 X 子文件夹,我需要上传文件。为此,我通过导航到 http://{sharepointsite}/_layouts/15/AppRegNew.aspx 生成了 client_id 和 client_secret。在 XML 权限中,我给出了以下代码:

我正在使用https://github.com/vgrem/Office365-REST-Python-Client进行此实现。当尝试使用以下代码片段查看时,如果我可以使用 client_id 和 client_secret 访问共享点,我会看到不同的错误:

    import json

from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.runtime.client_request import ClientRequest
from office365.runtime.utilities.request_options import RequestOptions
from office365.sharepoint.client_context import ClientContext


app_settings = {
    'url': 'https://companyURL.sharepoint.com/sites/A/B/Info_documents/C',
    'client_id': 'xxxxxxxx',
    'client_secret': 'xxxxxx',
}

context_auth = AuthenticationContext(url=app_settings['url'])
context_auth.acquire_token_for_app(client_id=app_settings['client_id'], client_secret=app_settings['client_secret'])

ctx = ClientContext(app_settings['url'], context_auth)
web = ctx.web
ctx.load(web)
ctx.execute_query()
print("Web site title: {0}".format(web.properties['Title']))

错误:ClientRequestException:('-2147024891,System.UnauthorizedAccessException','访问被拒绝。您无权执行此操作或访问此资源。','403 客户端错误:禁止访问 url:https://companyURL.sharepoint .com/sites/A/B/Info_documents/C_api/Web ')

但是我给了权限,不确定我做错了还是选择了错误的模块。

请帮忙。

以下是我在生成 client_ID 和 client_secret 时给出的 XML 代码:

<AppPermissionRequests AllowAppOnlyPolicy="true">
       <AppPermissionRequest Scope="http://sharepoint/content/sitecollection/web" Right="Read"/>
       <AppPermissionRequest Scope="http://sharepoint/content/sitecollection/web/list" Right="Write"/>
     </AppPermissionRequests>

标签: pythonpython-3.xrestms-accesssharepoint

解决方案


鉴于描述

我的目标是搜索文件夹 C,如果文件夹 C 中存在 X 子文件夹,我需要上传文件。

以及以下链接格式:

https://companyURL.sharepoint.com/sites/A/B/Info_documents/C

您的文件夹结构可以这样表示:

https://companyURL.sharepoint.com/sites/A/B/  <-this is the actual site url
          |
   Info_documents <-library
              |
          C <-folder

由于AuthenticationContextclass 接受第一个参数是site urlapp_settings因此需要像这样更新:

app_settings = {
    'url': 'https://companyURL.sharepoint.com/sites/A/B/',  //need to refer to site url
    'client_id': '--client id goes here--',
    'client_secret': '--client secret goes here--',
}

现在轮到App principal了,因为请求的权限适用于每个 web ( scope: http://sharepoint/content/sitecollection/web),第二步授予权限)需要每个指定的 web 完成:

https://companyURL.sharepoint.com/sites/A/B/_layouts/15/appinv.aspx

例子

这是一个演示如何验证父文件夹下是否存在子文件夹的示例:

context_auth = AuthenticationContext(url=app_settings['url'])
context_auth.acquire_token_for_app(client_id=app_settings['client_id'], 
client_secret=app_settings['client_secret'])
ctx = ClientContext(app_settings['url'], context_auth)

folder_url = "Info_documents/C"  #folder url where to find 
folder_name = "X"  #folder name to find
result = ctx.web.get_folder_by_server_relative_url(folder_url).folders.filter("Name eq '{0}'".format(folder_name))
ctx.load(result)
ctx.execute_query()
if len(result) > 0:
    print("Folder has been found: {0}".format(result[0].properties["Name"]))

推荐阅读