[英]Microsoft Graph Post action to Create group “Bad request”
我们正试图发布到微软图形API的请求创建一个组,像解释HERE
基本URL是: https : //graph.microsoft.com/v1.0/groups内容类型设置为apllication / json我们也有一个有效的Baerer令牌。
我们使用Microsoft.Graph命名空间(NuGet包)中的Group类,因此我们用数据填充属性,并调用JsonConvert.SerializeObject(group)将组对象序列化为Json。
这是我们建立和序列化的方式:
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;
HttpClient的标头设置如下:
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,标头或正文中存在错误,但在Graph Explorer中 ,与上面相同的代码可以正常工作,我们在响应中得到结果。 我要监督什么?
感谢您的任何反馈或建议。 亲切的问候。
由于您已经使用了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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.