You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python访问Office365 SharePoint时遇AADSTS90023错误求助

解决AADSTS90023: Invalid STS request错误

可能原因及修复步骤

1. 修正SharePoint站点URL

你当前使用的是页面级URL(SitePages/Home.aspx),但AuthenticationContext需要的是站点根地址而非具体页面。将URL修改为:

url = "https://{netorg_number}.sharepoint.com/sites/{company_name}"

2. 用户名密码认证方式失效(核心原因)

目前绝大多数组织会禁用Resource Owner Password Credentials (ROPc) 流程(即直接用户名密码认证),原因是该认证方式安全性低,且与MFA(多因素认证)完全不兼容。即便更换账号,只要组织禁用了该流程,就会触发此错误。

推荐改用以下两种更可靠的认证方式:

方式一:应用程序权限认证(Client Credentials)

适合后台服务、无需用户交互的场景:

  1. 在Azure AD中注册应用,授予SharePoint相关应用权限(如Sites.Read.All),并生成客户端密钥(Client Secret)。
  2. 使用以下代码认证:
from office365.sharepoint.client_context import ClientContext
from office365.runtime.auth.client_credential import ClientCredential

site_url = "https://{netorg_number}.sharepoint.com/sites/{company_name}"
client_id = "你的应用客户端ID"
client_secret = "你的应用客户端密钥"

ctx = ClientContext(site_url).with_credentials(ClientCredential(client_id, client_secret))
web = ctx.web
ctx.load(web)
ctx.execute_query()
print(f"站点标题:{web.properties['Title']}")

方式二:设备码认证(Device Code Flow)

适合需要用户交互认证、且账号启用MFA的场景:

  1. 在Azure AD中注册应用,设置为"公共客户端",并授予Delegated权限(如Sites.Read.All)。
  2. 使用以下代码认证:
from office365.sharepoint.client_context import ClientContext
from office365.runtime.auth.device_code_provider import DeviceCodeProvider

site_url = "https://{netorg_number}.sharepoint.com/sites/{company_name}"
client_id = "你的应用客户端ID"

provider = DeviceCodeProvider(client_id)
ctx = ClientContext(site_url).with_credentials(provider)
web = ctx.web
ctx.load(web)
ctx.execute_query()
print(f"认证成功,站点标题:{web.properties['Title']}")

运行后,会提示你访问指定URL并输入验证码,完成用户登录认证。

3. 检查组织Azure AD设置(仅针对坚持使用用户名密码的场景)

如果一定要使用用户名密码认证,需确认组织Azure AD是否允许ROPc流程:

  • 登录Azure门户,进入Azure AD > 企业应用程序 > 所有应用程序 > 找到你的应用 > 身份验证 > 确保"允许公共客户端流"开启(仅适用于公共客户端应用)。
  • 同时确认账号未启用MFA,且组织未禁用ROPc认证。

内容的提问来源于stack exchange,提问作者André Lourenço

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 06:50:23