1
votes

I am using the MVC Office 365 API libraries and I would like to archieve the following thing: Logging into User-Accounts where I know the username / password and then get there calendar entries.

What I have so far is code that makes this redirect and ask the user to enter credentials. But how can I log in for them wihtout asking? The idea is to get the calendar entries for every user (lets say 20 of them) automatically every few minutes.

public static async Task<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).ToArray();

    return events;
}

public static async Task<ExchangeClient> EnsureClientCreated()
{
    var _discoveryContext = await CacheHelper.GetDiscoveryContext();
    var dcr = await _discoveryContext.DiscoverResourceAsync(ServiceResourceId);
    return new ExchangeClient(ServiceEndpointUri, async () =>
    {
        return (await _discoveryContext.AuthenticationContext.AcquireTokenByRefreshTokenAsync(new SessionCache().Read("RefreshToken"),
            new Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential(_discoveryContext.AppIdentity.ClientId, _discoveryContext.AppIdentity.ClientSecret),
            ServiceResourceId))
            .AccessToken;
    });
}
1

1 Answers

0
votes

Late answer I know. But if your still looking for this, or anyone else, this blog may be what your looking for.

http://blogs.msdn.com/b/exchangedev/archive/2015/01/22/building-demon-or-service-apps-with-office-365-mail-calendar-and-contacts-apis-oauth2-client-credential-flow.aspx

A daemon/service app will get calendar events on behalf of a user, proving the user and the app are registered under the same tennat/organisation.