Django OAuth 2.0 Server App. Please fork and improve!
Python CSS JavaScript Shell
Latest commit 2a4371a Jun 14, 2014 @bhagany bhagany Update README.rst
Failed to load latest commit information.
docs Added authors file Mar 12, 2012
examples/mysite Remove trailing whitespace May 22, 2012
oauth2app Prefer simplejson, but fall back to json Nov 2, 2012
tests/testsite Prefer simplejson, but fall back to json Nov 2, 2012
.gitignore Ignore common virtualenv directories May 22, 2012
LICENSE.txt Initial commit. Jun 23, 2011 Add requirements.txt and; PEP8ify May 22, 2012
README.rst Initial commit. Jun 23, 2011
requirements.txt Prefer simplejson, but fall back to json Nov 2, 2012 Add requirements.txt and; PEP8ify May 22, 2012


Other projects have been able to devote the time and energy necessary to maintain oauth apps that we cannot, so while this library is not exactly unmaintained, you should probably be using something else. We highly recommend djoauth2 and Django OAuth Toolkit


If easy_install is available, you can use:



The oauth2app module helps Django site operators provide an OAuth 2.0 interface. The module is registered as an application.

In, add 'oauth2app' to INSTALLED_APPS.


Sync the DB models.

python syncdb

In, add /oauth2/authorize and /oauth2/token views to a new or existing app.

urlpatterns += patterns('',
    (r'^oauth2/missing_redirect_uri/?$',   'mysite.oauth2.views.missing_redirect_uri'),
    (r'^oauth2/authorize/?$',                'mysite.oauth2.views.authorize'),
    (r'^oauth2/token/?$',                    'oauth2app.token.handler'),

Create client models.

from oauth2app.models import Client

    name="My Sample OAuth 2.0 Client",

Create authorize and missing_redirect_uri handlers.

from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.template import RequestContext
from django.contrib.auth.decorators import login_required
from oauth2app.authorize import Authorizer, MissingRedirectURI, AuthorizationException
from django import forms

class AuthorizeForm(forms.Form):

def missing_redirect_uri(request):
    return render_to_response(

def authorize(request):
    authorizer = Authorizer()
    except MissingRedirectURI, e:
        return HttpResponseRedirect("/oauth2/missing_redirect_uri")
    except AuthorizationException, e:
        # The request is malformed or invalid. Automatically
        # redirects to the provided redirect URL.
        return authorizer.error_redirect()
    if request.method == 'GET':
        template = {}
        # Use any form, make sure it has CSRF protections.
        template["form"] = AuthorizeForm()
        # Appends the original OAuth2 parameters.
        template["form_action"] = '/oauth2/authorize?%s' % authorizer.query_string
        return render_to_response(
    elif request.method == 'POST':
        form = AuthorizeForm(request.POST)
        if form.is_valid():
            if request.POST.get("connect") == "Yes":
                # User agrees. Redirect to redirect_uri with success params.
                return authorizer.grant_redirect()
                # User refuses. Redirect to redirect_uri with error params.
                return authorizer.error_redirect()
    return HttpResponseRedirect("/")

Authenticate requests.

from oauth2app.authenticate import Authenticator, AuthenticationException
from django.http import HttpResponse

def test(request):
    authenticator = Authenticator()
        # Validate the request.
    except AuthenticationException:
        # Return an error response.
        return authenticator.error_response(content="You didn't authenticate.")
    username = authenticator.user.username
    return HttpResponse(content="Hi %s, You authenticated!" % username)

If you want to authenticate JSON requests try the JSONAuthenticator.

from oauth2app.authenticate import JSONAuthenticator, AuthenticationException

def test(request):
    authenticator = JSONAuthenticator()
        # Validate the request.
    except AuthenticationException:
        # Return a JSON encoded error response.
        return authenticator.error_response()
    username = authenticator.user.userame
    # Return a JSON encoded response.
    return authenticator.response({"username":username})


An example Django project demonstrating client and server functionality is available in the repository.