将REST AddMember绘制到组-错误的请求 [英] Graph REST AddMember to Group - Bad Request

查看:84
本文介绍了将REST AddMember绘制到组-错误的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将成员添加到组中.我可以列出组织中的所有组,通过电子邮件获取用户,获取所有用户,并且我什至可以从组中删除成员,但是我不能添加一个-返回的错误是400 Bad Request.

I am attempting to add members to a group. I am able to list all groups in my org, get user by email, get all users and I can even remove a Member from a group but I cannot add one - The error returned is 400 Bad Request.

以下是与工作功能相同的功能签名:(我确实有accesstoken,有效的组ID和有效的成员ID)

Here is the function which is the same function signature as those that work: (I do have the accesstoken, valid group id and a valid member id)

我已经确认身体数据至少在从

I have confirmed the body data looks correct at least as far as I can see from the example in the docs.

不确定我还可以添加些什么来使情况变得更清晰,询问并更新

public async Task<string> AddGroupMember(string accessToken, string groupId, string memberId)
{
    var status = string.Empty;
    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 body = new FormUrlEncodedContent(values);

    try
    {
        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;
}

感谢任何人都可以提供的帮助-这是我必须打的最后一个电话,然后免费上门服务

推荐答案

为所有关心未来的人找到了这个问题:

Found the issue for any who care to know for the future:

var body = new FormUrl...我的代码不正确,需要的是将一个简单的json字符串更改为此 UPDATED :

var body = new FormUrl... my code was incorrect, what's needed is a simple json string changed to this UPDATED:

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

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

我通常将值放在类中,但这是为了概念验证,并且json键需要看起来完全像这样@odata.id

I would normally put the values in a class but this is for proof of concept and the json key needs to look exactly like this @odata.id

这篇关于将REST AddMember绘制到组-错误的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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