cor*_*ngo 5 python authentication sharepoint 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())
Run Code Online (Sandbox Code Playgroud)
但我收到一条错误消息:
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 ''.
Run Code Online (Sandbox Code Playgroud)
我确实从多个设备(浏览器)连接到这个帐户,并且只有一次我被要求使用 MFA 登录(SMS 消息)。有没有办法解决这个问题?请注意,我不是系统管理员。
错误消息非常直观,启用多重身份验证 (MFA) 时不支持用户凭据身份验证。
为了避免此错误,可以改为使用SharePoint App-Only 流(由Office365-REST-Python-Clientlibrary支持)。
使用租户权限设置仅应用程序主体部分描述了如何配置它,总结它包括两个步骤:
创建并同意应用主体后,它可以用于访问 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']))
Run Code Online (Sandbox Code Playgroud)
以下是有关如何配置 SharePoint App-Only 流的说明:
注意:app主体注册操作(steps
1through5)需要每个租户执行一次。尽管6-9可以按租户或网站集应用授予权限的操作(步骤):
- 每个网站集授予的权限,需要网站集管理员(在提供的说明中,权限是每个网站集的授予者)
- 如果您更喜欢在租户级别授予权限,请访问租户管理站点,URL 必须包含
-admin以访问
租户管理站点,例如,
https://{tenant}-admin.sharepoint.com/_layouts/15/appinv.aspx。该操作需要租户管理员权限
脚步:
appregnew.aspxSharePoint Online 网站中的页面。例如,https://{tenant}.sharepoint.com/_layouts/15/appregnew.aspx。Python console。在App Domain 下,指定localhost. 在重定向 URI 下,指定https://localhost.注意:有时,如果您指定了实际域,例如
sharepoint.com在App Domain和Redirect URI字段中指定的域,而不是localhost,则An unexpected error has occurred可能会遇到错误消息。检查appregnew.aspx页面并确保两个字段都包含正确的localhostURI。
单击创建。
转到appinv.aspx网站集上的页面。例如,https://example.sharepoint.com/_layouts/15/appinv.aspx授予站点范围的权限。
在App Id字段中指定您的客户端 ID,然后单击查找以查找您的应用程序。要授予应用程序权限,请将下面的 XML 复制到应用程序的权限请求 XML 字段:
<AppPermissionRequests AllowAppOnlyPolicy="true">
<AppPermissionRequest Scope="http://sharepoint/content/sitecollection" Right="FullControl" />
</AppPermissionRequests>
Run Code Online (Sandbox Code Playgroud)
注意:对于租户级别范围,权限请求 XML 如下所示:
Run Code Online (Sandbox Code Playgroud)<AppPermissionRequests AllowAppOnlyPolicy="true"> <AppPermissionRequest Scope="http://sharepoint/content/tenant" Right="FullControl" /> </AppPermissionRequests>
| 归档时间: |
|
| 查看次数: |
6847 次 |
| 最近记录: |