Velin Georgiev Blog

Python/Flask Provider-Hosted app using Office365 SharePoint Site

21 Jan 2016
Tags: Python, Flask, Azure, Office365, SharePoint, SharePoint REST, OAuth, Provider-Hosted app, SharePoint Online
Velin Georgiev blog image

Flask and SharePoint is Fun!
I have created Python Flask scenario that shows how to set up authentication between a Python app (using the Flask microframework) and Office 365 SharePoint Online site. The goal of this sample is to show how a user can authenticate and interact with data from the Office 365 SharePoint site.
So I was able lift up the Python Flask app in Azure and to connect to Office 365 SharePoint Online site with around 100 lines of code :). How cool is that!
If you are not familiar with Flask, I highly recommend it for small projects with tight deadlines
Here are some prerequisites before we go into the code:
- Office 365 developer tenant
- Visual Studio 2015 installed and Python Tools for Visual Studio installed or favorite IDE, or just a good text editor :).
- Python 2.7 or 3.4 installed
- Flask, requests, PyJWT Python packages installed via pip

Prepare the scenario for the Office 365 Python Flask app authentication sample

The Office 365 Python Flask application will:
- Use Azure AD authorization endpoints to perform authentication
- Use Office 365 SharePoint API's to show the authenticated user's title

For these tasks to succeed you need to do additional setups explained below.
- Create Azure trial account with the Office 365 account so the app can be registered, or you can register it with PowerShell. A good tutorial can be found on this link
- Register the app in the Azure portal and assign http://localhost:5555 to the Sign-on URL and Reply URL
- Generate a client secret
- Grant the following permission to the Python Flask app: Office 365 SharePoint Online > Delegated Permissions > Read user profiles

Velin Georgiev blog image

- Copy the client secret and the client id from the Azure portal and replace them into the Python Flask config file
- Assign URL to the SharePoint site you are going to access to the RESOURCE config variable.

Velin Georgiev blog image

- Open the sample in Visual Studio 2015
- Go to Project > Properties > Debug and dedicate 5555 for Port Number

Velin Georgiev blog image

- Go to Python environments > your active python environment > execute "Install from requirements.txt". This will ensure that all the required Python packages are installed.

Velin Georgiev blog image

Run the Office 365 Python Flask app sample

When you run the sample you'll see the title and login url.

Velin Georgiev blog image

Once you've clicked the sign-in link, the Office 365 API will go through the authentication handshake and the Python Flask home screen will reload with the logged in user title and access token displayed:

Velin Georgiev blog image

Fork me on GitHub

Github link:

Comments (2):

Can you share the code for this? Thanks.
On 17 Feb 2016 by anonymous
I am sorry I forgot to add the link. Here it is:
On 17 Feb 2016 by anonymous
Add comment