Microsoft Graph API:(400)错误的请求

时间:2019-12-20 17:25:46

标签: c# microsoft-graph microsoft-graph-teams

我正在使用Microsoft Graph API通过POST https://graph.microsoft.com/v1.0/groups创建一个网上论坛,但收到了400 - Bad Request响应。

我的代码:

string url = AuthenticationRequest.Microsoft_Graph_API_URL + "/groups";
Dictionary<string, string> headers = new Dictionary<string, string>();
headers.Add("Authorization", "Bearer xxxxxxxxxxxxx");
WebRequests webReq = new WebRequests();
var ddd = new
{
    description = "Group to help readers",
    displayName = "Reader Assist",
    groupTypes = new List<String>()
    {
    "Unified"
    },
    mailEnabled = true,
    mailNickname = "rhelp",
    securityEnabled = false
};
string body = JsonConvert.SerializeObject(ddd);
string response = webReq.PostRequestWithheaders(url, headers, body);

方法PostRequestWithheaders

public string PostRequestWithheaders(string url, IDictionary<string, string> headers, string bodyData)
{
    try
    {
        System.Net.WebRequest webRequest = System.Net.WebRequest.Create(url);
        webRequest.Method = "POST";
        webRequest.ContentType = "application/json";
        if (headers.Count > 0)
        {
            foreach (var item in headers)
            {
                webRequest.Headers.Add(item.Key, item.Value);
            }
        }
        Stream dataStream = webRequest.GetRequestStream();
        byte[] postArray = System.Text.Encoding.ASCII.GetBytes(bodyData);
        dataStream.Write(postArray, 0, postArray.Length);
        dataStream.Close();
        WebResponse response = webRequest.GetResponse();
        dataStream = response.GetResponseStream();
        StreamReader responseReader = new StreamReader(dataStream);
        string returnValue = "";
        returnValue = responseReader.ReadToEnd().ToString();
        responseReader.Close();
        dataStream.Close();
        response.Close();
        return returnValue;

    }
    catch (Exception ex)
    {
        throw;
    }
    return "";
}

令牌是使用范围openid offline_access Calendars.ReadWrite.Shared profile email https://graph.microsoft.com/User.Read

生成的

任何人都可以在这里帮助解决这段代码的问题吗?

1 个答案:

答案 0 :(得分:1)

您的属性名称大小写不正确。它们都应该以小写开头:

var ddd = new
{
    description = "Group to help readers",
    displayName = "Reader Assist",
    groupTypes = new List<String>()
    {
    "Unified"
    },
    mailEnabled = true,
    sailNickname = "rhelp",
    securityEnabled = false
};