401 unauthorized while using django token authentication system

5,013 views
Skip to first unread message

Akash utreja

unread,
Dec 10, 2018, 8:33:36 AM12/10/18
to Django REST framework
Hi, I am using built in token authentication of django but I am getting weird error that some of my request got 200 and some of then got 401 unauthorized when  I run it on server same with localhost. Both request have same token value I've checked multiple times.Can anyone tell what is thing i am missing. please help me out here!
My request header is like Authorization: Token <token key>
Here is my settings.py file containing.
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.TokenAuthentication',
)}
Thanks   

CJ Keeney

unread,
Dec 10, 2018, 3:19:00 PM12/10/18
to django-res...@googlegroups.com
I believe if your SECRET_KEY setting is different between runs, the token would need to be recreated.

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Wanderley S

unread,
Dec 10, 2018, 5:33:32 PM12/10/18
to django-res...@googlegroups.com
Hi, did you add permission_classes in your view or in your settings? If not I believe you sholud do something like:
In your .views file:

from rest_framework.permissions import IsAuthenticated

Then inside your class:

Class SomeAPIView(APIView):
    permission_classes = (IsAuthenticated,)
.... the rest o your class....

Regards.


--

Arbaz Hundekar

unread,
Dec 11, 2018, 4:39:11 AM12/11/18
to django-res...@googlegroups.com
Check if your response is getting cached.
--
Warm Regards,

Arbaz

Akash utreja

unread,
Dec 12, 2018, 1:10:17 AM12/12/18
to Django REST framework
Hi thanks for reply.
Is it mandatory to put permission classes in each class? and what about functional views?
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.

Akash utreja

unread,
Dec 12, 2018, 1:11:39 AM12/12/18
to Django REST framework
Thanks for reply. It is same in each run. 
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.

Akash utreja

unread,
Dec 12, 2018, 1:12:08 AM12/12/18
to Django REST framework
Thanks for reply. I will check this.


On Tuesday, December 11, 2018 at 3:09:11 PM UTC+5:30, Arbaz Hundekar wrote:
Check if your response is getting cached.

On Tue, Dec 11, 2018 at 4:03 AM Wanderley S <wan...@gmail.com> wrote:
Hi, did you add permission_classes in your view or in your settings? If not I believe you sholud do something like:
In your .views file:

from rest_framework.permissions import IsAuthenticated

Then inside your class:

Class SomeAPIView(APIView):
    permission_classes = (IsAuthenticated,)
.... the rest o your class....

Regards.


Em Seg, 10 de dez de 2018 11:33, Akash utreja <utreja...@gmail.com escreveu:
Hi, I am using built in token authentication of django but I am getting weird error that some of my request got 200 and some of then got 401 unauthorized when  I run it on server same with localhost. Both request have same token value I've checked multiple times.Can anyone tell what is thing i am missing. please help me out here!
My request header is like Authorization: Token <token key>
Here is my settings.py file containing.
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.TokenAuthentication',
)}
Thanks   

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.


--
Warm Regards,

Arbaz

Wanderley S

unread,
Dec 12, 2018, 6:09:24 AM12/12/18
to django-res...@googlegroups.com
Hello,

No. You can set this "globally" in settings and then overight as needed in views (CBV, FBV)
Actually, for secutrity matters, is a best practice to do exactly like this:

First you close you API access in settings, by allowing access to administrators only, then, on each class or function you overright this.

"
REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAdminUser',
    )
}
"
Take a look at documentation, there are very good examples there.
https://www.django-rest-framework.org/api-guide/permissions/

Regards,

To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages