AAD B2C - 通过 post 将用户添加到 Azure 函数中的 GrapAPI

AAD B2C - Add user to group via post to GrapAPI in Azure function

在注册自定义策略中,创建用户后,我想将他或她添加到组中。我尝试以与我在登录策略中获取组成员身份相同的方式来执行此操作,并使用调用 GraphAPI 的自定义 Azure 函数。

出于测试目的,我首先尝试使用 Postman 调用 GraphAPI 以查看它是否有效。我按照文档让它工作并返回这个查询:

POST https://graph.microsoft.com/v1.0/groups/{{b2c-beneficiaire-group-id}}/members/$ref

Body: 
{
  "@odata.id": "https://graph.microsoft.com/v1.0/users/{{b2c-user-id}}"
}

而且效果很好。我收到 204 响应,用户实际上现在是该组的成员。

现在这是我尝试在我的 Azure 函数中复制它的部分:

        var url = $"https://graph.microsoft.com/v1.0/groups/{groupId}/members/$ref)";
        var keyOdataId = "@odata.id";
        var valueODataId = $"https://graph.microsoft.com/v1.0/users/{userId}";
        var bodyObject = new List<KeyValuePair<string, string>>
        {
            new KeyValuePair<string, string>(keyOdataId, valueODataId)
        };
        var jsonData = $@"{{ ""{keyOdataId}"": ""{valueODataId}"" }}";
        var groupBody = new StringContent(jsonData, Encoding.UTF8, "application/json");
        log.LogInformation($"{url} + body:{await groupBody.ReadAsStringAsync()}");

        using (var response = await httpClient.PostAsync(url, groupBody))
        {
            log.LogInformation("HttpStatusCode=" + response.StatusCode.ToString());
            if (!response.IsSuccessStatusCode)
            {
                throw new InvalidOperationException($"{response.StatusCode} - Reason:{response.ReasonPhrase}. Content:{await response.Content.ReadAsStringAsync()}");
            }   
        }

我尝试了一些变体(使用 HttpRequest 和其他东西),但我总是以 Odata 错误告终:

"BadRequest","message":"The request URI is not valid. Since the segment 'members' refers to a collection, 
this must be the last segment in the request URI or it must be followed by an function or action
that can be bound to it otherwise all intermediate segments must refer to a single resource."

据我所知,它与 OData 查询($ref 部分)有关。您是否知道我需要做什么才能让它发挥作用?

您的 url 似乎有错字,以 )

结尾
var url = $"https://graph.microsoft.com/v1.0/groups/{groupId}/members/$ref)";