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

Microsoft Graph API调用getMemberGroups返回400 Bad Request的原因是什么

调用getMemberGroups接口返回400的原因及修复方案

核心错误原因

  • 请求头与请求体格式不匹配:代码中声明了Content-Type: application/json请求头,但实际使用UrlEncodedFormEntity构造的是表单格式(application/x-www-form-urlencoded)的请求体,格式冲突导致服务端无法解析请求。
  • 传参格式不符合接口要求:getMemberGroups接口仅支持JSON格式的请求体,不接受表单键值对传参,接口要求的请求体结构为{"securityEnabledOnly": true}。

修复后的代码示例

HttpClient client = HttpClientBuilder.create().build();
HttpPost request = new HttpPost(url);
request.addHeader("Content-Type", "application/json");
request.addHeader("Authorization", "Bearer " + accessToken);

// 构造符合要求的JSON请求体
String requestBody = "{\"securityEnabledOnly\": true}";
request.setEntity(new StringEntity(requestBody, ContentType.APPLICATION_JSON));

HttpResponse response;
final StatusLine statusLine;
try {
    response = client.execute(request);
    statusLine = response.getStatusLine();
} catch (IOException e) {
    OutputUtil.printStacktrace(e);
    return null;
}

其他可选排查点

如果调整后依然返回400,可依次排查:

  • 访问令牌是否拥有该接口的调用权限,该接口需要至少User.Read.All、Group.Read.All级别的委托或应用权限
  • 请求url是否存在拼写错误,确认使用POST方法发起请求

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.16 20:45:08