【发布时间】:2018-07-05 18:24:03
【问题描述】:
我正在将 Outlook 日历与具有客户端凭据流的自定义日历集成,并且我正在尝试制作自己的 api,而不是完全使用 MAPI。
我想发帖到https://outlook.office.com/api/v2.0/TenantDomain/users/useremail@domain/events
我正在关注本指南Create 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 后,我现在收到此错误:
reason="获取访问令牌时使用的身份验证方法太弱,无法访问此应用程序。提供的身份验证强度为 1,要求为 2。";error_category="invalid_token"
现在我迷路了,accesstoken 需要在 json 对象的 header 中还是在 body 中?
更新 3 显然我需要知道我是如何获得 accessToken 的,如果我这次能做到,我会发布答案
感谢任何帮助!
【问题讨论】:
-
您确实需要在 HttpClient 中包含一个 accessToken
-
我做到了,var htttpclient = new HttpClient(); htttpclient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); htttpclient.DefaultRequestHeaders.Add("授权", "承载" + tokenn);但仍然给出 401
-
你是如何获得令牌的?
-
我是我自己创建的公司的管理员,所以首先我授权应用程序获取日历事件,我还添加了写入权限,授权后我发布到 azure 令牌端点并检索访问令牌。获取事件工作正常,但发布事件让我头疼..
-
看看使用微软的graph api。它可以让你做你想做的事,但它确实是很好的文档。
标签: c# asp.net json post outlook-restapi