Last active
June 28, 2019 16:45
-
-
Save ThorstenHans/e024c9e37b1167ea0c50 to your computer and use it in GitHub Desktop.
O365 API Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Office365.Exchange; | |
using Microsoft.Office365.OAuth; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace WebApplication2 | |
{ | |
public static class CalendarAPISample | |
{ | |
const string ExchangeResourceId = "https://outlook.office365.com"; | |
const string ExchangeServiceRoot = "https://outlook.office365.com/ews/odata"; | |
public static async Task<IOrderedEnumerable<IEvent>> GetCalendarEvents() | |
{ | |
var client = await EnsureClientCreated(); | |
// Obtain calendar event data | |
var eventsResults = await (from i in client.Me.Events | |
where i.End >= DateTimeOffset.UtcNow | |
select i).Take(10).ExecuteAsync(); | |
var events = eventsResults.CurrentPage.OrderBy(e => e.Start); | |
return events; | |
} | |
private static async Task<ExchangeClient> EnsureClientCreated() | |
{ | |
Authenticator authenticator = new Authenticator(); | |
var authInfo = await authenticator.AuthenticateAsync(ExchangeResourceId); | |
return new ExchangeClient(new Uri(ExchangeServiceRoot), authInfo.GetAccessToken); | |
} | |
public static void SignOut(Uri postLogoutRedirect) | |
{ | |
new Authenticator().Logout(postLogoutRedirect); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using System.Web; | |
using System.Web.Mvc; | |
namespace WebApplication2.Controllers | |
{ | |
public class HomeController : Controller | |
{ | |
public async Task<ActionResult> Index() | |
{ | |
var events = await CalendarAPISample.GetCalendarEvents(); | |
return View(events); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@model IEnumerable<Microsoft.Office365.Exchange.IEvent> | |
@{ | |
ViewBag.Title = "My Events"; | |
} | |
<h2>My Events</h2> | |
<table class="table table-striped"> | |
<tr> | |
<th>Subject</th> | |
<th>Start</th> | |
</tr> | |
@foreach (var item in Model) { | |
<tr> | |
<td> | |
@item.Subject | |
</td> | |
<td> | |
@item.Start | |
</td> | |
</tr> | |
} | |
</table> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello,
could you please let me know what's the assembly for the class Authenticator?
Thank you!!!