问题描述
对于多个帖子,我无法向 Microsoft Graph 发送帖子请求,我正在关注这个 文档,我正在尝试在
sorry for the multiple posts, I can't get a post request to Microsoft Graph to work, I'm following this documentation, I'm trying to create an event in
https://graph.microsoft.com/beta/myDomain/users/myEmail/日历/事件
我有一个函数和一些帮助类来创建 json 对象,如下所示:
I have a function and some helper classes to create the json object like so:
List<ToOutlookCalendar> toOutlook = new List<ToOutlookCalendar>();
toOutlook.Add(new ToOutlookCalendar
{
Start = new End
{
DateTime = DateTimeOffset.UtcNow,
TimeZone = "Pacific Standard Time"
},
End = new End
{
DateTime = DateTimeOffset.UtcNow,
TimeZone = "Pacific Standard Time"
},
Body = new Body
{
ContentType = "HTML",
Content = "testar for att se skit"
},
Subject = "testin",
Attendees = new List<Attendee>
{
new Attendee
{
EmailAddress = new EmailAddress
{
Address = "myEmail",
Name = "name"
},
Type = "Required"
}
},
Token = tokenn
});
return new JsonResult
{
Data = toOutlook
};
以前我发布到:https:///outlook.office.com/api/v2.0/myDomain/users/myEmail/calendar/events
这给了我一个错误 401,抱怨令牌是周.我创建了一个 x509 证书,但没有找到将其上传到我的 azure 目录的方法,因为我想以编程方式完成所有工作并且到目前为止已经成功,因此决定采用另一种方法并再次找到 Microsoft 图形文档.
which gave me an error 401, complaining about the token being to week. I created an x509 certificate but had no luck finding a way to upload it to my directory in azure and since I want to do everything programmatically and have succeeded so far and decided to take another approach and came upon the Microsoft graph documentation again.
我在授权 Calendars.ReadWrite 的应用程序权限后获取我的日历事件:https://graph.microsoft.com/beta/myDomain/users/myEmail/calendar/events.
I get my calendar events from after having authorized the application permissions for Calendars.ReadWrite: https://graph.microsoft.com/beta/myDomain/users/myEmail/calendar/events.
无论如何我的请求看起来像这样并给了我一个 400 Bad Request:
Anyhow my request looks like this and gives me a 400 Bad Request:
htttpclient.DefaultRequestHeaders.Add("Authorization", "Bearer " + tokenn);
htttpclient.DefaultRequestHeaders.Add("Host", "graph.microsoft.com");
htttpclient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var stringPayload = await Task.Run(() => JsonConvert.SerializeObject(res));
var response = await htttpclient.PostAsync($"https://graph.microsoft.com/beta/{myDomain}/users/{myEmail}/calendar/events",
new StringContent(stringPayload, Encoding.UTF8, "application/json"));
有人知道为什么吗?我正在按照我相信的信中的文档进行操作,但仍然收到 400 错误请求.
Does anyone have any idea why? I'm following the documentation to the letter i believe but still get a 400 bad request.
编辑 1
我使用这些类根据文档创建事件
I use these classes to create the event based on the documentation
public class ToOutlookCalendar
{
[JsonProperty("Subject")]
public string Subject { get; set; }
[JsonProperty("Body")]
public Body Body { get; set; }
[JsonProperty("Start")]
public End Start { get; set; }
[JsonProperty("End")]
public End End { get; set; }
[JsonProperty("Attendees")]
public List<Attendee> Attendees { get; set; }
}
public class Attendee
{
[JsonProperty("EmailAddress")]
public EmailAddress EmailAddress { get; set; }
[JsonProperty("Type")]
public string Type { get; set; }
}
public class EmailAddress
{
[JsonProperty("Address")]
public string Address { get; set; }
[JsonProperty("Name")]
public string Name { get; set; }
}
public class Body
{
[JsonProperty("ContentType")]
public string ContentType { get; set; }
[JsonProperty("Content")]
public string Content { get; set; }
}
public class End
{
[JsonProperty("DateTime")]
public DateTimeOffset DateTime { get; set; }
[JsonProperty("TimeZone")]
public string TimeZone { get; set; }
}
感谢任何帮助!
推荐答案
为了让这个更短/总结一下,这是我用来创建事件的,并且是必要的.
To make this a little shorter/summarize this is what i use to create an event and is necessary.
using (HttpClient c = new HttpClient())
{
String requestURI = "https://graph.microsoft.com/v1.0/users/"+userEmail+"/calendar/events.";
//with your properties from above except for "Token"
ToOutlookCalendar toOutlookCalendar = new ToOutlookCalendar();
HttpContent httpContent = new StringContent(JsonConvert.SerializeObject(toOutlookCalendar), Encoding.UTF8, "application/json");
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestURI);
request.Content = httpContent;
//Authentication token
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
var response = await c.SendAsync(request);
var responseString = await response.Content.ReadAsStringAsync();
}
您不需要这些标题:
htttpclient.DefaultRequestHeaders.Add("Host", "graph.microsoft.com");
htttpclient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
并且 Token 不是 Event 对象的一部分,因此您应该将其从 ToOutlookCalendar
中删除.
And Token is not Part of an Event object so you should remove it from ToOutlookCalendar
.
如果您不想一直自己编写所有这些 JSONObject,可以使用 Graph SDK for c#(也消除了您忘记属性/添加错误属性的风险).他们已经有一个名为 Event
的约会对象.
If you don't want to write all these JSONObjects yourself all the time you can use the Graph SDK for c# (also eliminates the risk that you forget a property/add a wrong one). They already have an object for Appointments called Event
.
编辑
您的请求 URL 中也不需要myDomain"部分.
You also don't need the "myDomain" part in your request URL.
这篇关于将事件发布到 Microsoft Graph c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!