为何调用MS Graph的getSharePointSiteUsageDetail接口无有效数据返回
问题根因与解决方案
你遇到的仅返回表头无数据的问题,按优先级排查以下几点:
- 微软365报表隐私设置限制:这是当前该类接口异常的最常见诱因。微软近期调整了报表默认隐私规则,默认隐藏所有站点、用户明细数据。
操作路径:进入Microsoft 365管理中心 → 选择「设置」→「组织设置」→「报告」→ 取消勾选「在所有报告中隐藏用户、组和站点名称」,配置完成后等待30分钟左右再调用接口验证。 - 请求头配置错误:
getSharePointSiteUsageDetail接口返回的是CSV格式数据,你代码中指定的application/json类型头不符合响应格式,可能引发解析异常,建议删除该参数。 - 编码解析优化:原有脚本用
Invoke-RestMethod处理CSV响应容易出现编码兼容问题,建议替换为Invoke-WebRequest手动处理返回内容的编码。 - 周期参数测试:可先将周期参数从
D180改为D7测试短周期接口是否正常返回数据,排除长周期数据生成异常的可能性。
修改后可运行的脚本片段
$AppId = "VALID-APP-ID-HERE" $TenantId = "VALID-TENANT-ID-HERE" $AppSecret = 'VALID-APP-SECRET-HERE' # 获取OAuth2访问令牌 $uri = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token" $body = @{ client_id = $AppId scope = "https://graph.microsoft.com/.default" client_secret = $AppSecret grant_type = "client_credentials" } $tokenRequest = Invoke-WebRequest -Method Post -Uri $uri -ContentType "application/x-www-form-urlencoded" -Body $body -UseBasicParsing $token = ($tokenRequest.Content | ConvertFrom-Json).access_token $headers = @{Authorization = "Bearer $token"} # 调用SharePoint站点使用明细接口 $SPOFilesReportsURI = "https://graph.microsoft.com/v1.0/reports/getSharePointSiteUsageDetail(period='D7')" $response = Invoke-WebRequest -Uri $SPOFilesReportsURI -Headers $headers -Method Get -UseBasicParsing # 处理UTF-8 BOM并转换为CSV $content = [System.Text.Encoding]::UTF8.GetString($response.Content).TrimStart([char]0xFEFF) $Sites = $content | ConvertFrom-Csv
内容的提问来源于stack exchange,提问作者Mathieu
相关产品推荐
相关产品推荐

