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

使用Microsoft Graph Python SDK获取SharePoint列表遇租户主机名错误

问题:Python版Microsoft Graph SDK访问SharePoint列表时提示“租户主机名无效”

存在类似问题,但本次情况不同——Python版Microsoft Graph SDK似乎无法直接指定MS365租户域名。

已创建具备足够权限的客户端密钥用于访问目标资源,尝试的代码及结果如下:

尝试代码

from azure.identity.aio import ClientSecretCredential
from msgraph import GraphServiceClient

# 认证信息
tenant_id         = 'abcdefgh-1234-5678-9012-abcdefghijkl'
app_client_id     = 'abcdefgh-1234-5678-9012-abcdefghijkl'
client_secret_val = 'lmnop~abcdefghijklmnopqrstuvwzxy'

# SharePoint站点ID和列表名称
site_id = 'mysite'
list_name = 'mylist'

# 构建客户端
credential = ClientSecretCredential(tenant_id, app_client_id, client_secret_val)
scopes = ['https://graph.microsoft.com/.default']
client = GraphServiceClient(credentials=credential, scopes=scopes)

# 获取数据
req_list = client.sites.by_site_id(site_id).lists.by_list_id(list_name)
res = await req_list.get()

错误输出

ODataError: 
        APIError
        Code: 400
        message: None
        error: MainError(
            additional_data={}, 
            code='invalidRequest', 
            details=None, 
            inner_error=InnerError(
                additional_data={}, 
                client_request_id='23691144-abe3-467d-b160-21c7ae84473b', 
                date=DateTime(2024, 2, 5, 17, 57, 8, tzinfo=Timezone('UTC')), 
                odata_type=None, 
                request_id='f69847e7-4f06-41b9-b2b2-5ce52e597364'
            ), 
            message='Invalid hostname for this tenancy', 
            target=None
        )

系统提示租户主机名无效,但代码里根本没用到主机名。而且Python版SDK比较新,找不到这些对象的可用方法和参数说明。

参考文档:

  • 获取SharePoint列表
  • Python版Microsoft Azure SDK

解决方法

错误根源是**site_id的格式完全错误**。Microsoft Graph要求的站点ID不是简单的站点名称mysite,必须使用符合规范的标识格式,常用的有两种:

  1. 完整的站点唯一ID:格式为{租户域名},{GUID1},{GUID2},比如contoso.sharepoint.com,abc123-def456-ghi789-jkl012-mno345pqrst67
  2. 域名+站点路径的组合格式:{租户域名}:/sites/{站点路径},比如contoso.sharepoint.com:/sites/mysite

如果不知道正确的站点ID,可以先调用API列出租户下的所有站点来获取:

# 列出租户内的站点,获取正确的站点ID
sites = await client.sites.get()
for site in sites.value:
    print(f"站点名称: {site.name}, 站点ID: {site.id}")

修正后的代码示例

from azure.identity.aio import ClientSecretCredential
from msgraph import GraphServiceClient

# 认证信息
tenant_id         = 'abcdefgh-1234-5678-9012-abcdefghijkl'
app_client_id     = 'abcdefgh-1234-5678-9012-abcdefghijkl'
client_secret_val = 'lmnop~abcdefghijklmnopqrstuvwzxy'

# 替换成你的租户域名和站点路径
site_id = 'contoso.sharepoint.com:/sites/mysite'
list_name = 'mylist'

# 构建客户端
credential = ClientSecretCredential(tenant_id, app_client_id, client_secret_val)
scopes = ['https://graph.microsoft.com/.default']
client = GraphServiceClient(credentials=credential, scopes=scopes)

# 获取列表数据
req_list = client.sites.by_site_id(site_id).lists.by_list_id(list_name)
res = await req_list.get()
print(res)

额外检查项

  • 确认应用已被授予Sites.Read.All或Sites.ReadWrite.All权限,且已完成管理员同意
  • 若使用的是私有站点,需确保应用权限覆盖该站点范围

内容的提问来源于stack exchange,提问作者MrChadMWood

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 17:04:51