【问题标题】:Microsoft Graph API: (400) Bad RequestMicrosoft Graph API:(400)错误请求
【发布时间】:2019-12-20 17:25:46
【问题描述】:

我正在使用 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生成Token

谁能帮忙看看这段代码有什么问题?

【问题讨论】:

  • 您已将此标记为microsoft-graph-sdks,但您的代码未使用 SDK?有什么原因吗?
  • 我看到你重新标记了这篇文章,但我的问题仍然存在;您没有为此使用 SDK 有什么原因吗?
  • 我从这篇帖子stackoverflow.com/questions/59451788/…得到了答案

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


【解决方案1】:

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

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

【讨论】:

  • 是的,我也使用了小写,但错误是一样的。无论如何,我也更新了我的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多