Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Consume Office 365 REST API Without UI

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.

like image 805
Christian Findlay Avatar asked Aug 31 '16 06:08

Christian Findlay


2 Answers

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); 
like image 102
Christian Findlay Avatar answered Oct 03 '22 10:10

Christian Findlay


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

like image 40
Eric Legault Avatar answered Oct 03 '22 09:10

Eric Legault