@@ -4,6 +4,9 @@ Getting started
44Django OAuth Toolkit provide a support layer for `Django REST Framework <http://django-rest-framework.org/ >`_.
55This tutorial is based on the Django REST Framework example and shows you how to easily integrate with it.
66
7+ **NOTE **
8+ The followin code has been tested with django 1.7.7 and Django REST Framework 3.1.1
9+
710Step 1: Minimal setup
811---------------------
912
@@ -44,27 +47,39 @@ Here's our project's root `urls.py` module:
4447
4548.. code-block :: python
4649
47- from django.conf.urls.defaults import url, patterns, include
50+ from django.conf.urls import url, patterns, include
4851 from django.contrib.auth.models import User, Group
4952 from django.contrib import admin
5053 admin.autodiscover()
5154
52- from rest_framework import viewsets, routers
53- from rest_framework import permissions
55+ from rest_framework import permissions, routers, serializers, viewsets
5456
5557 from oauth2_provider.ext.rest_framework import TokenHasReadWriteScope, TokenHasScope
5658
5759
60+ # first we define the serializers
61+ class UserSerializer (serializers .ModelSerializer ):
62+ class Meta :
63+ model = User
64+
65+
66+ class GroupSerializer (serializers .ModelSerializer ):
67+ class Meta :
68+ model = Group
69+
70+
5871 # ViewSets define the view behavior.
5972 class UserViewSet (viewsets .ModelViewSet ):
6073 permission_classes = [permissions.IsAuthenticated, TokenHasReadWriteScope]
61- model = User
74+ queryset = User.objects.all()
75+ serializer_class = UserSerializer
6276
6377
6478 class GroupViewSet (viewsets .ModelViewSet ):
6579 permission_classes = [permissions.IsAuthenticated, TokenHasScope]
6680 required_scopes = [' groups' ]
67- model = Group
81+ queryset = Group.objects.all()
82+ serializer_class = GroupSerializer
6883
6984
7085 # Routers provide an easy way of automatically determining the URL conf
@@ -98,22 +113,39 @@ Also add the following to your `settings.py` module:
98113 )
99114 }
100115
101- `OAUTH2_PROVIDER.SCOPES ` parameter contains the scopes that the application will be aware of,
116+ `OAUTH2_PROVIDER.SCOPES ` setting parameter contains the scopes that the application will be aware of,
102117so we can use them for permission check.
103118
104- Now run `python manage.py syncdb `, login to admin and create some users and groups.
119+ Now run the following commands:
120+
121+ ::
122+ python manage.py migrate
123+ python manage.py createsuperuser
124+ python manage.py runserver
125+
126+ The first command creates the tables, the second creates the admin user account and the last one
127+ runs the application.
128+
129+ Next thing you should do is to login in the admin at
130+
131+ ::
132+ http://localhost:8000/admin
133+
134+ and create some users and groups that will be queried later through our API.
135+
105136
106137Step 3: Register an application
107138-------------------------------
108139
109140To obtain a valid access_token first we must register an application. DOT has a set of customizable
110141views you can use to CRUD application instances, just point your browser at:
111142
112- `http://localhost:8000/o/applications/ `
143+ ::
144+ http://localhost:8000/o/applications/
113145
114- Click the button ` New Application ` and fill the form with the following data:
146+ Click on the link to create a new application and fill the form with the following data:
115147
116- * User : *your current user *
148+ * Name : *just a name of your choice *
117149* Client Type: *confidential *
118150* Authorization Grant Type: *Resource owner password-based *
119151
@@ -126,9 +158,9 @@ At this point we're ready to request an access_token. Open your shell
126158
127159::
128160
129- curl -X POST -d "grant_type=password&username=<user_name>&password=<password>" -u' <client_id>:<client_secret>' http://localhost:8000/o/token/
161+ curl -X POST -d "grant_type=password&username=<user_name>&password=<password>" -u" <client_id>:<client_secret>" http://localhost:8000/o/token/
130162
131- The *user_name * and *password * are the credential on any user registered in your :term: `Authorization Server `, like any user created in Step 2.
163+ The *user_name * and *password * are the credential of the users registered in your :term: `Authorization Server `, like any user created in Step 2.
132164Response should be something like:
133165
134166.. code-block :: javascript
@@ -162,7 +194,7 @@ Let's try to access resources using a token with a restricted scope adding a `sc
162194
163195::
164196
165- curl -X POST -d "grant_type=password&username=<user_name>&password=<password>&scope=read" http:// <client_id>:<client_secret>@ localhost:8000/o/token/
197+ curl -X POST -d "grant_type=password&username=<user_name>&password=<password>&scope=read" -u" <client_id>:<client_secret>" http:// localhost:8000/o/token/
166198
167199As you can see the only scope provided is `read `:
168200
0 commit comments