I'm developing an API using Django Rest Framework. I'm trying to list or create an "Order" object, but when i'm trying to access the console gives me this error:
{"detail": "Authentication credentials were not provided."}
Views:
from django.shortcuts import render
from rest_framework import viewsets
from django.contrib.auth.models import User
from rest_framework.renderers import JSONRenderer, YAMLRenderer
from rest_framework.response import Response
from rest_framework.views import APIView
from order.models import *
from API.serializers import *
from rest_framework.permissions import IsAuthenticated
class OrderViewSet(viewsets.ModelViewSet):
model = Order
serializer_class = OrderSerializer
permission_classes = (IsAuthenticated,)
Serializer:
class OrderSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Order
fields = ('field1', 'field2')
And my URLs:
# -*- coding: utf-8 -*-
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.contrib import admin
from django.utils.functional import curry
from django.views.defaults import *
from rest_framework import routers
from API.views import *
admin.autodiscover()
handler500 = "web.views.server_error"
handler404 = "web.views.page_not_found_error"
router = routers.DefaultRouter()
router.register(r'orders', OrdersViewSet)
urlpatterns = patterns('',
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
url(r'^api-token-auth/', 'rest_framework.authtoken.views.obtain_auth_token'),
url(r'^api/', include(router.urls)),
)
And then I'm using this command in the console:
curl -X GET http://127.0.0.1:8000/api/orders/ -H 'Authorization: Token 12383dcb52d627eabd39e7e88501e96a2sadc55'
And the error say:
{"detail": "Authentication credentials were not provided."}
If you are playing around in the command line (using curl, or HTTPie etc) you can use BasicAuthentication to test/user your API
You can then use curl
or httpie (its easier on the eyes):
or something else like Advanced Rest Client (ARC)
If you are runnig Django on Apache using mod_wsgi you have to add
in your httpd.conf. Otherwise authorization header will be stripped out by mod_wsgi.
Solved by adding "DEFAULT_AUTHENTICATION_CLASSES" to my settings.py
Just for other people landing up here with same error, this issue can arise if your
request.user
isAnonymousUser
and not the right user who is actually authorized to access the URL. You can see that by printing value ofrequest.user
. If it is indeed an anonymous user, these steps might help:Make sure you have
'rest_framework.authtoken'
inINSTALLED_APPS
in yoursettings.py
.Make sure you have this somewhere in
settings.py
:Make sure you have the correct token for the user who is logged in. If you do not have the token, learn how to get it here. Basically, you need to do a
POST
request to a view which gives you the token if you provide the correct username and password. Example:Make sure the view which you are trying to access, has these:
Use
curl
now this way:Example:
Adding SessionAuthentication in settings.py will do the job
REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework.authentication.SessionAuthentication', ), }
I too faced the same since I missed adding
in my API view class.
In addition to the above, we need to explicitly tell Django about the Authentication in settings.py file.