@@ -4,6 +4,9 @@ Getting started
4
4
Django OAuth Toolkit provide a support layer for `Django REST Framework <http://django-rest-framework.org/ >`_.
5
5
This tutorial is based on the Django REST Framework example and shows you how to easily integrate with it.
6
6
7
+ **NOTE **
8
+ The followin code has been tested with django 1.7.7 and Django REST Framework 3.1.1
9
+
7
10
Step 1: Minimal setup
8
11
---------------------
9
12
@@ -44,27 +47,39 @@ Here's our project's root `urls.py` module:
44
47
45
48
.. code-block :: python
46
49
47
- from django.conf.urls.defaults import url, patterns, include
50
+ from django.conf.urls import url, patterns, include
48
51
from django.contrib.auth.models import User, Group
49
52
from django.contrib import admin
50
53
admin.autodiscover()
51
54
52
- from rest_framework import viewsets, routers
53
- from rest_framework import permissions
55
+ from rest_framework import permissions, routers, serializers, viewsets
54
56
55
57
from oauth2_provider.ext.rest_framework import TokenHasReadWriteScope, TokenHasScope
56
58
57
59
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
+
58
71
# ViewSets define the view behavior.
59
72
class UserViewSet (viewsets .ModelViewSet ):
60
73
permission_classes = [permissions.IsAuthenticated, TokenHasReadWriteScope]
61
- model = User
74
+ queryset = User.objects.all()
75
+ serializer_class = UserSerializer
62
76
63
77
64
78
class GroupViewSet (viewsets .ModelViewSet ):
65
79
permission_classes = [permissions.IsAuthenticated, TokenHasScope]
66
80
required_scopes = [' groups' ]
67
- model = Group
81
+ queryset = Group.objects.all()
82
+ serializer_class = GroupSerializer
68
83
69
84
70
85
# Routers provide an easy way of automatically determining the URL conf
@@ -98,22 +113,39 @@ Also add the following to your `settings.py` module:
98
113
)
99
114
}
100
115
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,
102
117
so we can use them for permission check.
103
118
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
+
105
136
106
137
Step 3: Register an application
107
138
-------------------------------
108
139
109
140
To obtain a valid access_token first we must register an application. DOT has a set of customizable
110
141
views you can use to CRUD application instances, just point your browser at:
111
142
112
- `http://localhost:8000/o/applications/ `
143
+ ::
144
+ http://localhost:8000/o/applications/
113
145
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:
115
147
116
- * User : *your current user *
148
+ * Name : *just a name of your choice *
117
149
* Client Type: *confidential *
118
150
* Authorization Grant Type: *Resource owner password-based *
119
151
@@ -126,9 +158,9 @@ At this point we're ready to request an access_token. Open your shell
126
158
127
159
::
128
160
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/
130
162
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.
132
164
Response should be something like:
133
165
134
166
.. code-block :: javascript
@@ -162,7 +194,7 @@ Let's try to access resources using a token with a restricted scope adding a `sc
162
194
163
195
::
164
196
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/
166
198
167
199
As you can see the only scope provided is `read `:
168
200
0 commit comments