【发布时间】:2018-12-12 19:37:39
【问题描述】:
对于多个帖子感到抱歉,我无法向 Microsoft Graph 发送帖子请求,我正在关注此 documentation,我正在尝试在
中创建一个事件https://graph.microsoft.com/beta/myDomain/users/myEmail/calendar/events
我有一个函数和一些帮助类来创建 json 对象,如下所示:
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 图形文档。
我在授权 Calendars.ReadWrite:https://graph.microsoft.com/beta/myDomain/users/myEmail/calendar/events 的应用程序权限后获取我的日历事件。
无论如何我的请求看起来像这样并给了我一个 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 错误请求。
编辑 1
我使用这些类根据文档创建事件
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; }
}
感谢任何帮助!
【问题讨论】:
-
您不需要请求正文中的令牌(仅在标头中)。尝试从“ToOutlookCalendar”类中删除“Token”属性。
-
我完全忘记了 :) 但是我仍然收到错误 400 Bad Request
-
您可以尝试删除
htttpclient.DefaultRequestHeaders.Add("Host", "graph.microsoft.com"); htttpclient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));。这些应该不是必需的(StringContent中的 application/json 已经足够了)。 -
@MichaelHufnagel 你对 htttpclient.DefaultRequestHeaders.Add("Host", "graph.microsoft.com"); 是对的,这不是必需的,只是我添加的东西,但根据文档:htttpclient .DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));是必要的
-
是的,但是 PostAsync 中的 StringContent 应该已经将 Content-Type 标头设置为 application/json。 400 bad request 是否有任何错误消息传递?
标签: c# post office365 microsoft-graph-api outlook-restapi