首页 文章

Microsoft Graph Post操作以创建组“错误请求”

提问于
浏览
0

我们正在尝试将请求发布到Microsoft Graph API以创建一个组,如解释HERE

The base url is: https://graph.microsoft.com/v1.0/groups内容类型设置为apllication / json我们也有一个有效的Baerer令牌 .

我们使用Microsoft.Graph命名空间(NuGet包)中的Group类,因此我们使用我们的数据填充属性并调用JsonConvert.SerializeObject(group)将组objecet序列化为Json .

This is how we build up and serialze:

Microsoft.Graph.Group group = new Microsoft.Graph.Group();
                group.Description = "Self help community for library";
                group.DisplayName = "Library Assist";
                group.GroupTypes = new[] { "Unified" };
                group.MailEnabled = true;
                group.MailNickname = "library";
                group.SecurityEnabled = true;

   string json = JsonConvert.SerializeObject(group);

   var content = new StringContent(json);
   var response = httpclient.PostAsJsonAsync(Uri, content).Result;

The headers of the HttpClient are set like this:

httpclient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "...value of baerer token...");
 httpclient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

我们正在从 https://graph.microsoft.com/v1.0 开始构建URL,并将 /groups 添加到它

在回复中,我们得到了 Bad request status code 400 . 这意味着请求URI, Headers 或正文中存在错误,但在Graph Explorer中,与上面相同的代码工作正常,我们在响应中得到结果 . What am i overseeing?

感谢您的任何反馈或建议 . 亲切的问候 .

1 回答

  • 2

    由于您已经使用了 Microsoft.Graph 命名空间,因此可以使用内置的 GraphServiceClient 来发出请求,如下所示 . 您无需使用http客户端或序列化对象,这将被处理:

    var graphserviceClient = new GraphServiceClient(
        new DelegateAuthenticationProvider(
            (requestMessage) =>
            {
                requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", "<your-access-token>");              
            }));
    
    var group = new Microsoft.Graph.Group
    {
        DisplayName = "Library Assist",
        Description = "Self help community for library",
        MailNickname = "library",
        MailEnabled = true,
        SecurityEnabled = true,
        GroupTypes = new List<string> { "Unified" }
    };      
    
    var createdGroup = await graphserviceClient.Groups.Request().AddAsync(group);
    

    参考 - Intro to the Microsoft Graph .NET Client Library

相关问题