I keep getting a 403 error with Django OAuth Toolkit
I'm using Django OAuth toolkit to restrict access to API and I've followed this tutorial, but for some reason, DOT is restricting access to every request I make on it.
urls.py
:
from django.conf.urls import url, include
from django.contrib import admin
from rest_framework import routers
from api import views
router = routers.DefaultRouter()
admin.autodiscover()
from rest_framework import generics, permissions, serializers
from oauth2_provider.contrib.rest_framework import TokenHasReadWriteScope, TokenHasScope
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^api/v1/', include(router.urls)),
url(r'^api/v1/carfax/$', views.GetCarFax.as_view('get': 'list'), name='list'),
url(r'^api/v1/get_carfax/(?P<pk>[w-]+)/$', views.GetCarFax.as_view('get': 'retrieve'), name='retrieve'),
url(r'^api/v1/carfax/create/$', views.PostCarFax.as_view('post': 'create'), name='create'),
url('o/', include('oauth2_provider.urls', namespace='oauth2_provider')),
]
views.py:
class GetCarFax(viewsets.ModelViewSet):
''' This view will be used for POSTing new carfax reports to the database '''
queryset = CarFax.objects.all()
serializer_class = CarFaxSerializer
# authentication_classes =
permission_classes = [permissions.IsAuthenticated, TokenHasReadWriteScope, TokenHasScope]
#print('TEST')
# lookup_field = "vin"
def list(self, request):
# accessed at url: ^api/v1/carfax/$
queryset = CarFax.objects.all()
serializer = CarFaxSerializer(queryset, many=True)
return Response(serializer.data)
def retrieve(self, request, pk=None, *args, **kwargs):
# accessed at url: ^api/v1/retrieve/pk/$
queryset = CarFax.objects.all()
record = get_list_or_404(queryset, vin__exact=pk)
serializer = CarFaxSerializer(record, many=True)
return Response(serializer.data)
class PostCarFax(viewsets.ModelViewSet):
permission_classes = [permissions.IsAuthenticated, TokenHasReadWriteScope]
queryset = CarFax.objects.all()
serializer_class = CarFaxSerializer
My requests
file:
headers =
'Authorization': 'Bearer *****'
data =
"vin": test[0],
"structural_damage": test[2],
"total_loss": test[1],
"accident": test[5],
"airbags": 'TESTTTTT',
"odometer": test[4],
"recalls": test[6]
data = json.dumps(data)
response = requests.post('http://127.0.0.1:8000/api/v1/carfax/create/', data=data, headers=headers, cookies=cookies)
print(response.status_code)
return response
get-token.py
:
def authorize():
client_id = '***'
client_secret = '***'
data =
'grant_type': 'password',
'username': 'test1',
'password': 'test1',
response = requests.post('http://localhost:8000/o/token/', data=data, auth=(client_id, client_secret))
return response.text
I'm not sure exactly where I am going wrong here, the requests work when I remove the authentication. But otherwise it always throws a 403 forbidden error. I am getting the token successfully, the 403 error is basically telling me that my token doesn't grant me those permissions
python django oauth
add a comment |
I'm using Django OAuth toolkit to restrict access to API and I've followed this tutorial, but for some reason, DOT is restricting access to every request I make on it.
urls.py
:
from django.conf.urls import url, include
from django.contrib import admin
from rest_framework import routers
from api import views
router = routers.DefaultRouter()
admin.autodiscover()
from rest_framework import generics, permissions, serializers
from oauth2_provider.contrib.rest_framework import TokenHasReadWriteScope, TokenHasScope
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^api/v1/', include(router.urls)),
url(r'^api/v1/carfax/$', views.GetCarFax.as_view('get': 'list'), name='list'),
url(r'^api/v1/get_carfax/(?P<pk>[w-]+)/$', views.GetCarFax.as_view('get': 'retrieve'), name='retrieve'),
url(r'^api/v1/carfax/create/$', views.PostCarFax.as_view('post': 'create'), name='create'),
url('o/', include('oauth2_provider.urls', namespace='oauth2_provider')),
]
views.py:
class GetCarFax(viewsets.ModelViewSet):
''' This view will be used for POSTing new carfax reports to the database '''
queryset = CarFax.objects.all()
serializer_class = CarFaxSerializer
# authentication_classes =
permission_classes = [permissions.IsAuthenticated, TokenHasReadWriteScope, TokenHasScope]
#print('TEST')
# lookup_field = "vin"
def list(self, request):
# accessed at url: ^api/v1/carfax/$
queryset = CarFax.objects.all()
serializer = CarFaxSerializer(queryset, many=True)
return Response(serializer.data)
def retrieve(self, request, pk=None, *args, **kwargs):
# accessed at url: ^api/v1/retrieve/pk/$
queryset = CarFax.objects.all()
record = get_list_or_404(queryset, vin__exact=pk)
serializer = CarFaxSerializer(record, many=True)
return Response(serializer.data)
class PostCarFax(viewsets.ModelViewSet):
permission_classes = [permissions.IsAuthenticated, TokenHasReadWriteScope]
queryset = CarFax.objects.all()
serializer_class = CarFaxSerializer
My requests
file:
headers =
'Authorization': 'Bearer *****'
data =
"vin": test[0],
"structural_damage": test[2],
"total_loss": test[1],
"accident": test[5],
"airbags": 'TESTTTTT',
"odometer": test[4],
"recalls": test[6]
data = json.dumps(data)
response = requests.post('http://127.0.0.1:8000/api/v1/carfax/create/', data=data, headers=headers, cookies=cookies)
print(response.status_code)
return response
get-token.py
:
def authorize():
client_id = '***'
client_secret = '***'
data =
'grant_type': 'password',
'username': 'test1',
'password': 'test1',
response = requests.post('http://localhost:8000/o/token/', data=data, auth=(client_id, client_secret))
return response.text
I'm not sure exactly where I am going wrong here, the requests work when I remove the authentication. But otherwise it always throws a 403 forbidden error. I am getting the token successfully, the 403 error is basically telling me that my token doesn't grant me those permissions
python django oauth
1
Can you link to the tutorial by chance?
– Dakota Maker
Nov 13 '18 at 22:00
Sure here you go django-oauth-toolkit.readthedocs.io/en/latest/rest-framework/…
– Charles Smith
Nov 13 '18 at 22:02
add a comment |
I'm using Django OAuth toolkit to restrict access to API and I've followed this tutorial, but for some reason, DOT is restricting access to every request I make on it.
urls.py
:
from django.conf.urls import url, include
from django.contrib import admin
from rest_framework import routers
from api import views
router = routers.DefaultRouter()
admin.autodiscover()
from rest_framework import generics, permissions, serializers
from oauth2_provider.contrib.rest_framework import TokenHasReadWriteScope, TokenHasScope
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^api/v1/', include(router.urls)),
url(r'^api/v1/carfax/$', views.GetCarFax.as_view('get': 'list'), name='list'),
url(r'^api/v1/get_carfax/(?P<pk>[w-]+)/$', views.GetCarFax.as_view('get': 'retrieve'), name='retrieve'),
url(r'^api/v1/carfax/create/$', views.PostCarFax.as_view('post': 'create'), name='create'),
url('o/', include('oauth2_provider.urls', namespace='oauth2_provider')),
]
views.py:
class GetCarFax(viewsets.ModelViewSet):
''' This view will be used for POSTing new carfax reports to the database '''
queryset = CarFax.objects.all()
serializer_class = CarFaxSerializer
# authentication_classes =
permission_classes = [permissions.IsAuthenticated, TokenHasReadWriteScope, TokenHasScope]
#print('TEST')
# lookup_field = "vin"
def list(self, request):
# accessed at url: ^api/v1/carfax/$
queryset = CarFax.objects.all()
serializer = CarFaxSerializer(queryset, many=True)
return Response(serializer.data)
def retrieve(self, request, pk=None, *args, **kwargs):
# accessed at url: ^api/v1/retrieve/pk/$
queryset = CarFax.objects.all()
record = get_list_or_404(queryset, vin__exact=pk)
serializer = CarFaxSerializer(record, many=True)
return Response(serializer.data)
class PostCarFax(viewsets.ModelViewSet):
permission_classes = [permissions.IsAuthenticated, TokenHasReadWriteScope]
queryset = CarFax.objects.all()
serializer_class = CarFaxSerializer
My requests
file:
headers =
'Authorization': 'Bearer *****'
data =
"vin": test[0],
"structural_damage": test[2],
"total_loss": test[1],
"accident": test[5],
"airbags": 'TESTTTTT',
"odometer": test[4],
"recalls": test[6]
data = json.dumps(data)
response = requests.post('http://127.0.0.1:8000/api/v1/carfax/create/', data=data, headers=headers, cookies=cookies)
print(response.status_code)
return response
get-token.py
:
def authorize():
client_id = '***'
client_secret = '***'
data =
'grant_type': 'password',
'username': 'test1',
'password': 'test1',
response = requests.post('http://localhost:8000/o/token/', data=data, auth=(client_id, client_secret))
return response.text
I'm not sure exactly where I am going wrong here, the requests work when I remove the authentication. But otherwise it always throws a 403 forbidden error. I am getting the token successfully, the 403 error is basically telling me that my token doesn't grant me those permissions
python django oauth
I'm using Django OAuth toolkit to restrict access to API and I've followed this tutorial, but for some reason, DOT is restricting access to every request I make on it.
urls.py
:
from django.conf.urls import url, include
from django.contrib import admin
from rest_framework import routers
from api import views
router = routers.DefaultRouter()
admin.autodiscover()
from rest_framework import generics, permissions, serializers
from oauth2_provider.contrib.rest_framework import TokenHasReadWriteScope, TokenHasScope
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^api/v1/', include(router.urls)),
url(r'^api/v1/carfax/$', views.GetCarFax.as_view('get': 'list'), name='list'),
url(r'^api/v1/get_carfax/(?P<pk>[w-]+)/$', views.GetCarFax.as_view('get': 'retrieve'), name='retrieve'),
url(r'^api/v1/carfax/create/$', views.PostCarFax.as_view('post': 'create'), name='create'),
url('o/', include('oauth2_provider.urls', namespace='oauth2_provider')),
]
views.py:
class GetCarFax(viewsets.ModelViewSet):
''' This view will be used for POSTing new carfax reports to the database '''
queryset = CarFax.objects.all()
serializer_class = CarFaxSerializer
# authentication_classes =
permission_classes = [permissions.IsAuthenticated, TokenHasReadWriteScope, TokenHasScope]
#print('TEST')
# lookup_field = "vin"
def list(self, request):
# accessed at url: ^api/v1/carfax/$
queryset = CarFax.objects.all()
serializer = CarFaxSerializer(queryset, many=True)
return Response(serializer.data)
def retrieve(self, request, pk=None, *args, **kwargs):
# accessed at url: ^api/v1/retrieve/pk/$
queryset = CarFax.objects.all()
record = get_list_or_404(queryset, vin__exact=pk)
serializer = CarFaxSerializer(record, many=True)
return Response(serializer.data)
class PostCarFax(viewsets.ModelViewSet):
permission_classes = [permissions.IsAuthenticated, TokenHasReadWriteScope]
queryset = CarFax.objects.all()
serializer_class = CarFaxSerializer
My requests
file:
headers =
'Authorization': 'Bearer *****'
data =
"vin": test[0],
"structural_damage": test[2],
"total_loss": test[1],
"accident": test[5],
"airbags": 'TESTTTTT',
"odometer": test[4],
"recalls": test[6]
data = json.dumps(data)
response = requests.post('http://127.0.0.1:8000/api/v1/carfax/create/', data=data, headers=headers, cookies=cookies)
print(response.status_code)
return response
get-token.py
:
def authorize():
client_id = '***'
client_secret = '***'
data =
'grant_type': 'password',
'username': 'test1',
'password': 'test1',
response = requests.post('http://localhost:8000/o/token/', data=data, auth=(client_id, client_secret))
return response.text
I'm not sure exactly where I am going wrong here, the requests work when I remove the authentication. But otherwise it always throws a 403 forbidden error. I am getting the token successfully, the 403 error is basically telling me that my token doesn't grant me those permissions
python django oauth
python django oauth
edited Nov 14 '18 at 1:27
Joel
1,5706719
1,5706719
asked Nov 13 '18 at 21:54
Charles SmithCharles Smith
6471918
6471918
1
Can you link to the tutorial by chance?
– Dakota Maker
Nov 13 '18 at 22:00
Sure here you go django-oauth-toolkit.readthedocs.io/en/latest/rest-framework/…
– Charles Smith
Nov 13 '18 at 22:02
add a comment |
1
Can you link to the tutorial by chance?
– Dakota Maker
Nov 13 '18 at 22:00
Sure here you go django-oauth-toolkit.readthedocs.io/en/latest/rest-framework/…
– Charles Smith
Nov 13 '18 at 22:02
1
1
Can you link to the tutorial by chance?
– Dakota Maker
Nov 13 '18 at 22:00
Can you link to the tutorial by chance?
– Dakota Maker
Nov 13 '18 at 22:00
Sure here you go django-oauth-toolkit.readthedocs.io/en/latest/rest-framework/…
– Charles Smith
Nov 13 '18 at 22:02
Sure here you go django-oauth-toolkit.readthedocs.io/en/latest/rest-framework/…
– Charles Smith
Nov 13 '18 at 22:02
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290091%2fi-keep-getting-a-403-error-with-django-oauth-toolkit%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290091%2fi-keep-getting-a-403-error-with-django-oauth-toolkit%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Can you link to the tutorial by chance?
– Dakota Maker
Nov 13 '18 at 22:00
Sure here you go django-oauth-toolkit.readthedocs.io/en/latest/rest-framework/…
– Charles Smith
Nov 13 '18 at 22:02