首页 > 解决方案 > 使用 python 进行 Azure 共享点多因素身份验证

问题描述

我正在尝试使用 python 下载一个托管在共享点中的 excel 文件,该共享点是Microsoft Azure 平台的一部分。共享点受密码保护,我有一个帐户和密码,可用于通过浏览器登录,

为了使用 python 脚本进行身份验证,我遵循了以下建议的方法:Sharepoint authentication with python。它使用O365 rest python 客户端库,如下所示:

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


url = 'https://organization.sharepoint.com/sites/something/somepage.aspx'
username = 'userx@organization.com'
password = 'fakepass'

ctx_auth = AuthenticationContext(url)
if ctx_auth.acquire_token_for_user(username, password):
    ctx = ClientContext(url, ctx_auth)

else:
    print(ctx_auth.get_last_error())

但我收到一条错误消息:

An error occurred while retrieving token: AADSTS50076: Due to a configuration
change made by your administrator, or because you moved to a new location, you
must use multi-factor authentication to access ''.

我确实从多个设备(浏览器)连接到此帐户,并且只有一次我需要使用 MFA 登录(短信)。有没有办法解决这个问题?请注意,我不是系统的管理员。

标签: pythonazureauthenticationsharepoint

解决方案


错误消息非常直观,启用多重身份验证 (MFA) 时不支持用户凭据身份验证。

为了规避此错误,可以改用SharePoint App-Only 流(受Office365-REST-Python-Clientlibrary支持)。

使用租户权限设置仅应用程序主体部分描述了如何配置它,总结它包括两个步骤:

  1. 注册 App 主体(将其视为“服务帐户”)
  2. 授予权限

创建并同意应用主体后,即可使用它来访问 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。尽管可以按租户或网站集应用授予权限的操作(步骤):56-9

  • 每个网站集授予的权限,并且需要网站集管理员(在提供的说明中,权限是每个网站集的授予者)
  • 如果您希望在租户级别授予权限,请访问租户管理站点,URL 必须包含-admin访问
    租户管理站点,例如,
    https://{tenant}-admin.sharepoint.com/_layouts/15/appinv.aspx. 该操作需要租户管理员权限

脚步:

  1. 转到appregnew.aspx您的 SharePoint Online 网站中的页面。例如,https://{tenant}.sharepoint.com/_layouts/15/appregnew.aspx
  2. 在此页面上,单击Client IDClient Secret字段旁边的Generate按钮以生成它们的值。
  3. 安全地存储客户端 ID 和客户端密码,因为这些凭据可用于读取或更新 SharePoint Online 环境中的所有数据。您还将使用它们在应用程序中配置 SharePoint Online 连接。
  4. Title下,指定一个标题。例如,Python console。在应用程序域下,指定localhost。在重定向 URI下,指定https://localhost

注意:有时,如果您指定一个实际域,例如sharepoint.comApp DomainRedirect URI字段中指定域,而不是,则可能会遇到localhost错误消息。An unexpected error has occurred检查appregnew.aspx页面并确保两个字段都包含正确的localhostURI。

  1. 单击创建

  2. 转到appinv.aspx网站集上的页面。例如,https://example.sharepoint.com/_layouts/15/appinv.aspx授予站点范围的权限。

  3. App Id字段中指定您的客户端 ID ,然后单击查找以查找您的应用程序。要向应用授予权限,请将以下 XML 复制到应用的权限请求 XML 字段:

<AppPermissionRequests AllowAppOnlyPolicy="true">
<AppPermissionRequest Scope="http://sharepoint/content/sitecollection" Right="FullControl" />
</AppPermissionRequests>

注意:对于租户级别范围,权限请求 XML 如下所示:

<AppPermissionRequests AllowAppOnlyPolicy="true">
<AppPermissionRequest Scope="http://sharepoint/content/tenant" Right="FullControl" />
</AppPermissionRequests>
  1. 单击创建
  2. 在确认对话框中,单击Trust It 以授予权限。

推荐阅读