【问题标题】:Microsoft Sharepoint Authentication using Python使用 Python 的 Microsoft Sharepoint 身份验证
【发布时间】:2019-09-26 17:45:25
【问题描述】:

我正在尝试将文档上传到在线 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 进行此实现。当尝试使用以下代码 sn-p 查看时,如果我可以使用 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>

【问题讨论】:

    标签: python python-3.x rest ms-access sharepoint


    【解决方案1】:

    根据描述

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

    以及以下链接格式:

    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
    

    由于AuthenticationContext class 接受第一个参数是 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,因为请求的权限适用于每个网站 (scope: http://sharepoint/content/sitecollection/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"]))
    

    【讨论】:

    • 谢谢@Vadim。我已按照您的指示进行操作,但很遗憾,我仍然无法进行身份验证。它给了我 ClientRequestException:('-2147024891,System.UnauthorizedAccessException','访问被拒绝。您无权执行此操作或访问此资源。',“403 客户端错误:禁止 url:companyURL.sharepoint.com/sites/A/C/_api/Web/…)。我确实将 XML 文件权限更改为读写
    • @Srini,您很可能错过了授予权限步骤,请务必访问https://companyURL.sharepoint.com/sites/A/B/_layouts/15/appinv.aspx 页面,提供client id 作为App Id、AppPermissionRequests 并单击创建。如果一切按预期进行,应该会出现“信任”窗口,其中应该单击 Trust It 按钮(以同意权限)。
    猜你喜欢
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 2018-12-02
    • 2010-11-07
    • 1970-01-01
    • 1970-01-01
    • 2021-09-14
    • 2021-11-10
    相关资源
    最近更新 更多