gpt4 book ai didi

python - 使用 python 进行 Azure 共享点多重身份验证

转载 作者:行者123 更新时间:2023-12-01 07:57:07 28 4
gpt4 key购买 nike

我正在尝试使用 python 下载托管在 sharepoint 中的 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 = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b7c2c4d2c5cff7d8c5d0d6d9decdd6c3ded8d999d4d8da" rel="noreferrer noopener nofollow">[email protected]</a>'
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 登录(短信)。有办法解决这个问题吗?请注意,我不是系统的管理员。

最佳答案

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

为了避免此错误,SharePoint App-Only flow可以改为使用(由 Office365-REST-Python-Client library 支持)。

Setting up an app-only principal with tenant permissions部分描述了如何配置它,概括起来包括两个步骤:

  1. 注册应用主体(将其视为“服务帐户”)
  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 仅应用程序流程的说明:

Note: app principal registration operation(steps 1 through 5)needs to be performed once per tenant. Although the operation forgranting permissions ( steps 6-9) could be applied either per tenantor site collection:

  • permissions granted per site collection and requires a site collection administrator (in the provided instruction the permissionsare granter per site collection)
  • If you prefer to grant permissions on tenant level, visit tenant administration site instead, the URL must include -admin to access
    the tenant administration site, for example,
    https://{tenant}-admin.sharepoint.com/_layouts/15/appinv.aspx. Thatoperation requires a tenant administrator permissions

步骤:

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

Note: Sometimes, if you specify a actual domain, e.g. sharepoint.com domain in the App Domain and Redirect URI fields, instead of localhost, the error message An unexpected error has occurred might encounter. Check the appregnew.aspx page and make sure both fields include the proper localhost URI.

  • 点击创建

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

  • 应用 ID 字段中指定您的客户端 ID,然后点击“查找”来查找您的应用。要向应用程序授予权限,请将以下 XML 复制到应用程序的权限请求 XML 字段:

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

    Note: For tenant level scope, permission request XML looks as follows:

    <AppPermissionRequests AllowAppOnlyPolicy="true">
    <AppPermissionRequest Scope="http://sharepoint/content/tenant" Right="FullControl" />
    </AppPermissionRequests>
  • 点击创建
  • 在确认对话框中,点击信任它以授予权限。
  • 关于python - 使用 python 进行 Azure 共享点多重身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55922791/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com