[英]Post event to Outlook Calendar programmatically 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中包含accessToken嗎?
更新資料
我已在請求標頭中添加了令牌,但仍未授權401:
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + tokenn);
更新2在標頭中包含accessToken之后,現在出現此錯誤:
原因=“訪問令牌是使用認證方法獲得的,該方法太弱而無法允許對此應用程序進行訪問。當前身份驗證強度為1,要求為2。” ;; error_category =“ invalid_token”
現在我迷路了,訪問令牌是否需要位於標頭或json對象的主體中?
更新3,我需要ududate我如何獲得accessToken,如果這次我能做到的話,我會發布答案
任何幫助表示贊賞!
這個問題已經在另一篇文章中得到了解答,請點擊此鏈接獲取答案。 我在這里使用了錯誤的requestURL,在另一篇文章中解釋了everytnig :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.