以编程方式将事件发布到 Outlook 日历 c# ASP.NET



我正在将 Outlook 日历与具有客户端凭据流的自定义日历集成,我正在尝试制作自己的 api,而不是大量使用 MAPI。

我想发帖给 https://outlook.office.com/api/v2.0/TenantDomain/users/useremail@domain/events

我正在按照本指南创建事件并制作了帮助程序类:

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; }
}

我的 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 = "some email",
Name = "name"
}
},
new Attendee
{
EmailAddress = new EmailAddress
{
Address = "some email",
Name = "name"
}
}
}
});
return new JsonResult
{
Data = toOutlook,
ContentType = "application/json",
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};

我想做的是制作一个像这样的 PostAsync 方法:

var res =  await ToOutlookKalendrar();
var stringPayload = await Task.Run(() => JsonConvert.SerializeObject(res));
var httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/json");
var responsse = await client.PostAsync($"https://outlook.office.com/api/v2.0/{tenantDomain}/users/{userEmail}/events", stringPayload );

但是这给了我 401 未经授权,我错过了什么吗? 我需要在 Httpclient 中包含访问令牌吗?

更新

我已经在请求标头中添加了令牌,但仍然未经授权收到 401:

client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + tokenn);

更新 2在标头中包含访问令牌后,我现在收到此错误:

reason="访问令牌是使用身份验证方法获取的,该方法太弱,不允许访问此应用程序。呈现的身份验证强度为 1,所需的身份验证强度为 2。error_category="invalid_token">

现在我迷路了,访问令牌需要位于 json 对象的标头中还是正文中?

更新 3显然我需要说明我如何获得访问令牌,如果我这次设法做对了,我会发布答案

任何帮助不胜感激!!

这个问题已经在另一篇文章中得到了回答,请点击此链接获取答案。我在这里使用了错误的请求URL,每个帖子都在另一篇文章中进行了解释:)

最新更新