使用Python访问Office365 Sharepoint REST端点(Office365 Sharepoint REST client for Python)

2024-05-29 10:11:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从Python访问Sharepoint站点,而AADSTS似乎有一个条件访问策略阻止我获取令牌

是否有人使用Office365 Python Rest客户端成功连接到Sharepoint Rest端点

这就是我看到的实际错误

"C:\Python\envs\dev\python.exe c:\Users\xxxxxx.vscode\extensions\ms-python.python-2020.8.105369\pythonFiles\lib\python\debugpy\launcher 52801 -- c:\xxxx\Code\PythonSharepointUpdate\readsharepoint.py " An error occurred while retrieving token from XML response: AADSTS53003: Access has been blocked by Conditional Access policies. The access policy does not allow token issuance. An error occurred while retrieving auth cookies from https://microsoft.sharepoint.com/_vti_bin/idcrl.svc/
I am trying to execute this code from the Sample

    from office365.runtime.auth.user_credential import UserCredential 
    from office365.sharepoint.client_context import ClientContext 
    from office365.runtime.auth.authentication_context import AuthenticationContext 
    site_url = 'https://microsoft.sharepoint.com/teams/' 
    ctx = ClientContext(site_url).with_credentials(UserCredential("user@domain.com", "My Complex Password”)) 
    web = ctx.web 
    ctx.load(web) 
    ctx.execute_query() 
    print("Web title: {0}".format(web.properties['Title']))

另外,有没有更好的方法来验证Sharepoint?我讨厌在代码中以明文形式键入密码☹

有人使用ClientCredential吗?Sharepoint网站上是否有单独的设置来启用ClientCredential?我们的Sharepoint允许我们这样做吗


Tags: fromimportcomauthanrestweberror
1条回答
网友
1楼 · 发布于 2024-05-29 10:11:02

诀窍是使用ClientCredentials而不是UserCredentials

您必须在Sharepoint网站上创建一个新应用程序,该应用程序具有新的CliendID和客户端密码。创建新应用程序后,必须为新创建的应用程序授予网站权限。设置权限后,您必须信任该应用程序

完整的解释和步骤记录在这里https://github.com/vgrem/Office365-REST-Python-Client/wiki/How-to-connect-to-SharePoint-Online-and-and-SharePoint-2013-2016-2019-on-premises with-app-principal

相关问题 更多 >

    热门问题