6
votes

I need to push calendar entries in to a client's Outlook account. This is fairly straight forward with Exchange. You just authenticate with a user that has access, and then you can push entries in to other user's accounts. It seems to be completely different in Office 365.

I tried to follow the instructions here: https://dev.outlook.com/restapi/getstarted

I created the app and got the app's client ID. But, all of the documentation is around oAuth. Generally speaking, oAuth is designed for scenarios when a user needs to enter their credentials in through a browser window that will then confirm with the user which credentials they are willing to allow the app to have.

This does not match my scenario. I need to be able to push the calendar entries in to the account without any UI. This is back end integration. It just needs to do its job silently.

I looked at this sample app: https://github.com/OfficeDev/O365-Win-Snippets

But, this is a front end app. When it needs to authenticate, it pops up a window to force the user to enter their credentials.

When I try to call the REST API that is mentioned in the getting started page, it returns HTML. This is the Url it mentions:

https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=&redirect_uri=http%3A%2F%2Flocalhost%2Fmyapp%2F&response_type=code&scope=https%3A%2F%2Foutlook.office.com%2Fmail.read

I've tried a few permutations of this Url with my client ID. I've tried passing in my Office 365 credentials through basic http authentication.

I'm stuck.

3
You can try to store the access token you retrieve when the user logs in, and use that access token to perform requests to the API. The access token is valid for 1 hour I believe. You probably want to use the new Microsoft Graph api btw: graph.microsoft.io/en-us/docs/overview/overviewkoelkastfilosoof
It doesn't work that way. This is a back end system. We don't have any of the user's outlook credentials and we never will have. In exchange we just had admin privileges to push calendar entries in to their inbox.Christian Findlay
I think I've come to realise that the Office API is a set of client APIs that just essentially allows you to automate the client software - not do admin type stuff in the back end. I guess that that's what the exchange services are for. So, really I think I've been on a wild goose chase. Can I use the exchange API to administer a groups's Office 365 accounts?Christian Findlay

3 Answers

5
votes

The answer is simple. Use the Exchange API - not Office 365 API.

I was confused because I assumed that Office 365 was a different entity to Exchange, but the Office 365 email server just is one giant Exchange server. Here's some sample code for good measure. This is an example of logging in to Office 365's Exchange server and sending off a calendar entry to an email address. Simple.

I made a wild guess about the exchange Url and it was correct: https://outlook.office365.com/ews/exchange.asmx

    //Connect to exchange
    var ewsProxy = new ExchangeService(ExchangeVersion.Exchange2013);
    ewsProxy.Url = new Uri("https://outlook.office365.com/ews/exchange.asmx");

    //Create the meeting
    var meeting = new Appointment(ewsProxy);

    ewsProxy.Credentials = new NetworkCredential(_Username, _Password);
    meeting.RequiredAttendees.Add(_Recipient);

    // Set the properties on the meeting object to create the meeting.
    meeting.Subject = "Meeting";
    meeting.Body = "Please go to the meeting.";
    meeting.Start = DateTime.Now.AddHours(1);
    meeting.End = DateTime.Now.AddHours(2);
    meeting.Location = "Location";
    meeting.ReminderMinutesBeforeStart = 60;

    // Save the meeting to the Calendar folder and send the meeting request.
    meeting.Save(SendInvitationsMode.SendToAllAndSaveCopy); 
1
votes

My understanding is that this is possible, but the authentication looks quite complicated. For starters, any application that requires Office 365 integration must also integrate with the associated Azure AD. You can register your application for specific users so that it has the permissions required for whatever operations you need to perform. See here for a good summary of this component: https://msdn.microsoft.com/en-us/office/office365/howto/connect-your-app-to-o365-app-launcher?f=255&MSPPError=-2147217396#section_2

For authentication, you require a daemon/server application model. I've not attempted this yet, but it's documented here and looks like it should meet your needs (see the Daemon or Server Application to Web API section): https://azure.microsoft.com/en-us/documentation/articles/active-directory-authentication-scenarios/#daemon-or-server-application-to-web-api

-1
votes

In order to call the Office 365 REST API, the app requires an access token from Azure Active Directory, that's why you need (mandatory) to register app in Microsoft Azure Active Directory (Azure AD). Your Office 365 account in turn needs to be associated with Azure AD. This answer summarizes on how to register app in Azure AD in order to consume Office 365 API.

Basic authentication scheme

Regrading Basic authentication, currently it is enabled for API version 1.0, the following example demonstrates how to consume Outlook Calendar REST API in .NET application.

Prerequisites:

domain: https://outlook.office365.com/
API version: v1.0

Here is an example that gets my calendars and prints its names

private static async Task ReadCalendars()
{
    var handler = new HttpClientHandler();
    handler.Credentials = new NetworkCredential()
    {
        UserName = ConfigurationManager.AppSettings["UserName"],
        Password = ConfigurationManager.AppSettings["Password"]
    };

    using (var client = new HttpClient(handler))
    {
        var url = "https://outlook.office365.com/api/v1.0/me/calendars";
        var result = await client.GetStringAsync(url);

        var data = JObject.Parse(result);
        foreach (var item in data["value"])
        {
            Console.WriteLine(item["Name"]);
        }
    }
}