使用 Azure AD API 将成员添加到通讯组

Add member to distribution group using Azure AD API

我想将成员添加到通讯组列表。显然我无法使用 Microsoft Graph 执行此操作,我正在尝试使用 Azure AD Graph API。我正在使用 Node.js.

我可以使用 adal-node 库连接到 Azure。我取回令牌、发送请求并获得响应。 (我可以列出组、用户等)。

我正在关注Add Members documentation,但我很困惑。

  1. 在URL, object_id Group的id是我要加成员吗?

  2. 对于myorganization,我使用的是tennant_id

  3. 在哪里指定用户数据?我应该在 POST 中传递它吗?如果是这样,格式是什么?

  4. URL中的$links是什么?

目前,我正在这样做:

request.post(
  "https://graph.windows.net/TENNANT_ID_HERE/groups/GROUP_ID_HERE/$links/members?api-version=1.6",
  {
    headers: {
      Authorization: "Bearer " + TOKEN_HERE,
      "Content-Type": "application/json"
    },
    form: { key: "value" } //should I put my user object here?
  },
  function(err, res, body) {
    if (err) {
      console.log("err: " + err);
    } else {
      console.log("res: " + JSON.stringify(res, null, 3));
    }
  }
);

我收到以下错误:

{
  "odata.error": {
    "code": "Request_BadRequest",
    "message": {
      "lang": "en",
      "value":  "A supported MIME type could not be found that matches the 
                content type of the response. None of the supported type(s) 'application/xml, text/xml, 
                application/json;odata=minimalmetadata;streaming=true, application/json;odata=minimalmetadata;
                streaming=false, application/json;odata=minimalmetadata, 
                application/json;odata=fullmetadata;streaming=true, 
                application/json;odata=fullmetadata;streaming=false, 
                application/json;odata=fullmetadata, 
                application/json;odata=nometadata;streaming=true, 
                application/json;odata=nometadata;streaming=false, 
                application/json;odata=nometadata, 
                application/json;streaming=true, 
                application/json;streaming=false, 
                application/json;odata=verbose, 
                application/json' 
                matches the content type 'application/x-www-form-urlencoded'."
    }
  }
}

我们可以将成员添加到具有 AD graph API 的组。

post https://graph.windows.net/{tenantId}/groups/{groupobjectid}/$links/members?api-version=1.6

正文

{
  "url": "https://graph.windows.net/{tenantId}/directoryObjects/{userObjectId}"
}

使用 Postman 进行测试

short/most 重要的答案是 Microsoft Graph 或 Azure AD Graph API 都不支持通讯组列表。来自 documentation:

Important: You can only add members to security groups and mail-enabled security groups.


也就是说,从技术上讲,这不是您的呼叫在这里失败的原因。由于您正在使用的组的类型,您的代码已经到了失败的地步。虽然它不会帮助您管理通讯组列表,但以下是实际发生的情况。

form: { key: "value" } 选项告诉 request 将有效负载作为 URL 编码形式 (application/x-www-form-urlencoded) 发送。 API 要求有效载荷作为 JSON (application/json) 发送。

要发送 JSON,您需要做两件事:

  1. json 选项设置为 true
  2. 设置 body 值而不是 form 值。

正确的代码应该是这样的:

request.post(
  "https://graph.windows.net/{tenant-id}/groups/{group-id}/$links/members?api-version=1.6",
  {
    headers: {
      Authorization: "Bearer " + TOKEN_HERE
    },
    json: true,
    body: JSON.stringify({ url: "https://graph.windows.net/{tenant-id}/directoryObjects/{user-id}" })
  },
  function(err, res, body) {
    if (err) {
      console.log("err: " + err);
    } else {
      console.log("res: " + JSON.stringify(res, null, 3));
    }
  }
);

URI 中的 $links 参数告诉 API 您正在向另一个资源(在本例中为用户记录)提供 link。