【发布时间】:2021-10-15 17:51:03
【问题描述】:
我正在尝试使用仅限 Azure AD 应用程序连接到 SharePoint Online 网站。我正在使用 Office365-REST-Python-Client v2.3.5 (https://github.com/vgrem/Office365-REST-Python-Client)。
我已按照以下链接的说明通过 Azure AD App-Only 授予访问权限:https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azuread。 API 权限 Sites.FullControl.All 已通过 Microsoft Azure 门户中的“应用程序权限”授予我的应用程序。我还确保已向 API 权限授予管理员许可。
我能够成功访问我的 SharePoint Online 根站点 https://{my_tenant_name}.sharepoint.com 并将文件上传到文档库“文档”而没有任何问题。但是,如果我尝试访问除我的根网站以外的另一个 SharePoint Online 网站 https://{my_tenant_name}.sharepoint.com/sites/test,我会收到以下错误:
“AADSTS500011:在名为 {tenant_guid} 的租户中找不到名为 https://{my_tenant_name}.sharepoint.com/sites/test 的资源主体。如果应用程序的管理员尚未安装该应用程序,则可能会发生这种情况租户或租户中的任何用户同意。您可能将身份验证请求发送给了错误的租户。”
我已经仔细检查了 SharePoint Online 网站 URL、租户 guid 和 API 权限,包括管理员的同意,但我不确定是什么导致了错误。
下面是我可以重现问题的代码:
from office365.sharepoint.client_context import ClientContext
# Access to the SharePoint Online root site works
# site_url = 'https://{my_tenant_name}.sharepoint.com'
# Other than SharePoint Online root site gives AADSTS500011 error
site_url = 'https://{my_tenant_name}.sharepoint.com/sites/test'
app_settings = {
'tenant': '{my_tenant_name}.onmicrosoft.com',
'client_id': '<$guid>',
'thumbprint': "<$base64CertHash>",
'certificate_path': 'selfsigned_certificate.pem',
}
ctx = ClientContext(site_url).with_client_certificate(
app_settings.get('tenant'),
app_settings.get('client_id'),
app_settings.get('thumbprint'),
app_settings.get('certificate_path')
)
current_web = ctx.web
ctx.load(current_web)
ctx.execute_query()
print("current_web.url: {}".format(current_web.url))
【问题讨论】:
标签: python azure sharepoint-online office365api