This is a bare minimum example of a Flask application that authenticates using an Office 365 account using OAuth2. Most examples I found in Microsoft's getting started documentaiton for Python all involve Django. As I am primarily a Flask user I wrote this template using my preferred framework.
The app contains a single page with a login button. This will take you to the login page for Office 365 and then redirect you back. With a successful authentication the new API token will be stored in a local SQL database for use by the application and also displayed on the web page.
- Python 2.7 tested (2.7.10)
- Flask (0.11.1)
- Flask-SQLAlchemy (2.1)
- Requests (2.11.1)
You can quickly install these requirements using the included requirements.txt
file:
$ pip install -r requirements.txt
If you have a devleoper account with Office 365 the process for creating the app with the correct permissions is fairly straightforward.
- Go to https://portal.azure.com and sign in with your developer account.
- Navigate to Azure Active Directory > App Registrations > Add
- Enter the Name for your app
- Select Web app / API for the Application Type
- Enter the Sign-on URL (use http://127.0.0.1:5000 if testing locally)
- Once the app has been created go to All settings > (General) Reply URLs and update the listed URL to include /connect/get_token/ (example: http://127.0.0.1:5000/connect/get_token)
- In All settings go to (API Access) Required Permissions > Add
- Select the appropriate APIs and Permissions required for your app (note that access to Windows Azure Active Directory > Sign in and read user profile is already selected for your app to authenticate users).
- In ALl Settings go to (API Access) Keys
- Enter the Key description and select an Expires option
- Click Save and your new key will be displayed. YOU MUST COPY THIS VALUE NOW - IT WILL NOT APPEAR AGAIN!
Copy the values for the application ID and key into the config.py
file's O365_APP_ID
and O365_APP_KEY
variables. Change the SERVER
value to your server's full address.
Localhost example:
...
SERVER = 'http://127.0.0.1'
...
O365_APP_ID = '46ed0493-0ccb-47e7-a587-670936ca0d26'
O365_APP_KEY = 'NTU2YmUxNDUtMWJkNS00MjFhLTgzOGQtZjUzYTY5MTE='
...
Run the app locally by executing run.py
:
$ python run.py
Access the app at http://127.0.0.1:5000