c# - Microsoft Graph API:(400)错误请求
问题描述
我正在使用 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 "";
}
Token 是使用作用域生成的openid offline_access Calendars.ReadWrite.Shared profile email https://graph.microsoft.com/User.Read
任何人都可以在这里帮助此代码有什么问题吗?
解决方案
您的属性名称大小写不正确。它们都应该以小写字母开头:
var ddd = new
{
description = "Group to help readers",
displayName = "Reader Assist",
groupTypes = new List<String>()
{
"Unified"
},
mailEnabled = true,
sailNickname = "rhelp",
securityEnabled = false
};
推荐阅读
- javascript - 将 withAuthenticator 组件与来自 react-redux 的连接一起使用
- kivy - 切换到其他应用程序或锁定屏幕时如何在后台在android上运行kivy应用程序
- android - Firebase addOnSuccessListener 上的 Kotlin CountDownLatch 未解锁
- api - 我坚持在 vue 上显示这个 api
- django - 数据不会使用基于分类的视图从 Django 表单提交到数据库
- pip - 在 continuumio/miniconda3:4.9.2 映像上安装 PyICU
- php - 按当前页面从 Curl 处理 XML
- python - 从字典列表中找到最大值
- arrays - 如何处理 Typescript 中的稀疏数组和未定义数组?
- node.js - 如何用猫鼬区分填充的集合