Getting started

Installation

$ pip install django-oauth2-provider

Configuration

Add OAuth2 Provider to INSTALLED_APPS

INSTALLED_APPS = (
    # ...
    'provider',
    'provider.oauth2',
)

Modify your settings to match your needs

The default settings are available in provider.constants.

Include the OAuth 2 views

Add provider.oauth2.urls to your root urls.py file.

url(r'^oauth2/', include('provider.oauth2.urls', namespace = 'oauth2')),

Note

The namespace argument is required.

Sync your database

$ python manage.py syncdb
$ python manage.py migrate

How to request an access token for the first time ?

Create a client entry in your database

Note

To find out which type of client you need to create, read Section 2.1.

To create a new entry, simply use the django admin panel.

Request an access token

Your client interface – I mean by that your iOS code, HTML code, or whatever else language – just have to submit a POST request at the url /oauth2/access_token with the following fields :

  • client_id the client id you’ve just configured at the previous step.
  • client_secret again configured at the previous step.
  • username the username with which you want to log in.
  • password well, that speaks for itself.

This is only one way to authenticate with OAuth 2, there is other methods but I will only show you the PasswordGrant type one in this quick “Getting started” guide.

Note

Remember that you SHOULD always use HTTPS for all your OAuth 2 requests otherwise you won’t be secured.

Now you can use the command line to check that your local configuration is working :

$ curl -X POST -d "client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&grant_type=password&username=YOUR_USERNAME&password=YOUR_PASSWORD" http://localhost:8000/oauth2/access_token/

Here is the response you should get :

{"access_token": "<your-access-token>", "scope": "read", "expires_in": 86399, "refresh_token": "<your-refresh-token>"}