执行 Microsoft Graph API 的 POST 请求以将成员添加到 AD 组 [英] Executing POST request for Microsoft Graph API to add members to an AD group

查看:18
本文介绍了执行 Microsoft Graph API 的 POST 请求以将成员添加到 AD 组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将成员添加到通过 Azure 函数调用 Microsoft Graph API 的 AD 组

I am trying to add members to an AD groups invoking Microsoft Graph API through an Azure Function

通过 Graph API 执行 GET 请求非常简单直接,但我找不到任何示例如何执行 Graph API 的 post 请求

It is very easy and straightforward to execute GET requests through Graph API's, but I can't find any examples how I could execute post requests for the Graph API

我确实有一个 Graph API 的发布请求示例

I do have an example of a post request for the Graph API which is

POST https://graph.microsoft.com/v1.0/groups/{id}/members/$ref
Content-type: application/json
Content-length: 30

{
  "@odata.id": "https://graph.microsoft.com/v1.0/directoryObjects/{id}"
}

这是我成功用于检索 Graph 响应的代码

Here is the code I successfully use to retrieve the Graph response

public static async Task<HttpResponseMessage> GetDirectoryUsers(string graphToken, TraceWriter log, string displayName)
        {
            HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", graphToken);

            string requestUrl = "https://graph.microsoft.com/v1.0/groups?$top=2&$filter=displayName eq '" + displayName + "'&$expand=Members";
            var request = new HttpRequestMessage(new HttpMethod("GET"), requestUrl);
            var response = await client.SendAsync(request);
            return response;
        }

但是,我完全不知道如何通过 Azure 函数中的 C# 代码执行请求,以将检索到的用户添加到另一个 AD.如何构造请求 URL?我应该如何处理该请求 URL 中的 odata id?

However, I am completely lost how I could execute the request through a C# code within the Azure function to ADD the retrieved users to another AD. How can construct the request URL? How should I handle the odata id within that request URL?

如果有人能以任何方式帮助我,我将不胜感激

If anyone could help me in any way, I would greatly appreciate it

推荐答案

这是对我有用的答案

public static async Task<string> AddGroupMember(string accessToken, string groupId, string memberId)
{
    var status = string.Empty;
    try
    {
        string endpoint = "https://graph.microsoft.com/v1.0/groups/" + groupId + "/members/$ref";
        string queryParameter = "";

        // pass body data 
        var keyOdataId = "@odata.id";
        var valueODataId = "https://graph.microsoft.com/v1.0/directoryObjects/" + memberId;

        var values = new List<KeyValuePair<string, string>>
{
    new KeyValuePair<string, string>(keyOdataId, valueODataId)
};
        var jsonData = $@"{{ ""{keyOdataId}"": ""{valueODataId}"" }}";
        var body = new StringContent(jsonData, Encoding.UTF8, "application/json");


        using (var client = new HttpClient())
        {
            using (var request = new HttpRequestMessage(HttpMethod.Post, endpoint + queryParameter))
            {
                request.Content = body;
                request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

                using (var response = await client.SendAsync(request))
                {
                    if (response.StatusCode == HttpStatusCode.NoContent)
                        status = "Member added to Group";
                    else
                        status = $"Unable to add Member to Group: {response.StatusCode}";
                }
            }
        }
    }
    catch (Exception ex)
    {
        status = $"Error adding Member to Group: {ex.Message}";
    }

    return status;
}

这篇关于执行 Microsoft Graph API 的 POST 请求以将成员添加到 AD 组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆