Skip to content

Instantly share code, notes, and snippets.

@ThorstenHans
Last active June 28, 2019 16:45
Show Gist options
  • Save ThorstenHans/e024c9e37b1167ea0c50 to your computer and use it in GitHub Desktop.
Save ThorstenHans/e024c9e37b1167ea0c50 to your computer and use it in GitHub Desktop.
O365 API Sample
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);
}
}
}
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);
}
}
}
@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>
@EmilioNT
Copy link

EmilioNT commented Jul 7, 2017

Hello,
could you please let me know what's the assembly for the class Authenticator?

Thank you!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment