该错误消息非常直观,启用了多重身份验证(MFA)时不支持 用户凭据 身份验证。
为了避免此错误,可以改用SharePoint仅应用程序流(由Office365-REST-Python- Client
library支持)。
设置具有租户权限的仅应用程序主体,本节介绍如何配置它,并概括起来包括两个步骤:
一旦创建并同意了应用程序主体,就可以利用它来访问SharePoint资源,如下所示:
from office365.sharepoint.client_context import ClientContext
from office365.runtime.auth.client_credential import ClientCredential
site_url = 'https://contoso.sharepoint.com/'
app_principal = {
'client_id': '--client-id-goes-here--',
'client_secret': '--client-secret-goes-here--',
}
credentials = ClientCredential(app_principal['client_id'], app_principal['client_secret'])
ctx = ClientContext(url).with_credentials(credentials)
web = ctx.web
ctx.load(web)
ctx.execute_query()
print("Web site title: {0}".format(web.properties['Title']))
这是有关如何配置SharePoint仅应用程序流的说明:
注意:应用程序主登记操作(步骤1
通过5
)需要进行 每个租户一次 。尽管可以针对6-9
每个租户或网站集应用授予权限的操作(步骤):
https://{tenant}-admin.sharepoint.com/_layouts/15/appinv.aspx
。该操作需要租户管理员权限
脚步:
注意:有时,如果您指定实际域,例如sharepoint.com
“ 和“ 字段中的域,而不是localhost
,则An unexpected error has occurred
可能会遇到错误消息。检查appregnew.aspx
页面并确保两个字段都包含正确的localhost
URI。
点击 。
转到appinv.aspx
网站集上的页面。例如,https://example.sharepoint.com/_layouts/15/appinv.aspx
授予 站点范围的 权限。
在“ *
注意:对于 级别范围,权限请求XML如下所示:
<AppPermissionRequests AllowAppOnlyPolicy="true">
<AppPermissionRequest Scope="http://sharepoint/content/tenant"
Right=”FullControl” />