How To Read Microsoft Outlook Calendars with Python

Eric Pietrowicz
5 min readNov 20, 2019

Follow along with my latest posts here.

Using the O365 Python library makes communicating with the Microsoft Outlook API very straight forward. The following steps are taken to set up the environment in which you may query either your own Outlook calendar or a calendar that has been shared with you in your network.

The docs for the library can be found at the link:

Let’s start by navigating to the Microsoft Azure portal at the following link:

Sign in with your organization’s account, then open the “App Registrations” page.

Azure Portal

Create a new registration:

App registrations window

On the next page, set the web redirect URI to: https://login.microsoftonline.com/common/oauth2/nativeclient

Redirect URI

Fill in the remaining required fields and click “register” at the bottom of the page. Once registered, there are two tokens that are required in order to use the API. First, take note of the Application (Client) ID on the next page.

This will be required when using the O365 library

Next, we need to generate a Secret Key. Click “Certificates & secrets” on the toolbar to the left.

Certificates and secrets tab

Click on “New client secret”. Fill in a description and set the expiration to “never”. A new line will be added under the Client secrets section with your description, expiration date, and value. Copy the secret key in the value column and save it. This will be the last time you have access to it, so it may be worth storing in another secure location.

Navigate to the API permissions tab and click “Add a permission”.

API Permissions

Choose the Microsoft Graph API.

API Selection

Clicking through, we have two options: delegated permissions, or application permissions.

With the delegated permissions, the user will be required to validate the script with a console authentication. This requires user interaction every time the script is launched. We’ll choose this option for now, so this will be explained further.

Application permissions will not require user interaction, however, each permission requested will require an admin approval. So in an corporate setting, this might cause some difficulties.

Scroll through the list of options until you reach “Calendars”. Choose both calendar read items.

Requesting API permissions

Now we’re ready to move on to the Python script which will use this app’s permissions to query a calendar on our network.

Start by installing the library.

pip install O365

At the top of our script, make some imports from the library and set the client ID and secret ID we grabbed from the Azure portal.

Next, define the protocol being used and the scopes of your app. This corresponds to the API permissions we granted above. The protocol is the type of API we chose, and the scopes represent the permissions we granted ourselves. If the scope is outside of those defined in the Azure portal, the program will throw an error.

Also note, passing a default_resource to the protocol will determine which shared calendar we are pointed at. If it is left blank, the output will be the calendar of the user logged during the authentication step.

Pass these values into an account instance.

Then try to authenticate the object with our given scopes.

When our program tries to authenticate the account, the console output will contain a link to a login portal. Once the user is successfully logged in, pasting the return link will all for the script to continue. The console output might look something like the following:

Visit the following url to give consent:
https://login.microsoftonline.com/common/oauth2/v2.0/authorize?response_type=code&client_id=61950bbf-c1ca-47cf-b8e9-8c80669aa55a&redirect_uri=https%3A%2F%2Flogin.microsoftonline.com%2Fcommon%2Foauth2%2Fnativeclient&scope=Calendars.Read.Shared&state=h6ZFjpuYB4xm3TWoQ4MW6aykAo5iv5&access_type=offline
Paste the authenticated url here:

Next, a schedule instance needs to be declared from the account.

Then, a calendar instance can be grabbed from the schedule.

The list of events that correspond to the calendar returns from the calendar instance. Include_recurring defaults to true, in which case you need to pass in a query. For example:

If include_recurring is set to false, a query variable is not required.

Print out each event.

The output is in the following format:

Subject: Appointment (on: 2019-11-06 from: 13:30:00 to: 14:30:00)

With some string manipulation, we can get the subject or time of the event. For example, a function that would return the time remaining before the next event might look something like this:

This is a short example of how a shared calendar can be queried using the Microsoft Graph API library. There are plenty of additional features this library offers that are outside the scope of this article. Take a look through the documentation for more ideas.

--

--