Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Rest Framework authenticate header

Using Django REST API, I'm trying to authenticate my request.

This is what I'm trying to send:

Content-Type: application/json, Authentication: token="6d82549b48a8b079f618ee9c51a6dfb59c7e2196" 

This is what I get back:

{"detail": "Authentication credentials were not provided."} 

Could someone give me the correct header?

Thanks

The Header:

Accept: application/json Content-Type: application/json Authorization: Token 6d82549b48a8b079f618ee9c51a6dfb59c7e2196 Connection: keep-alive Origin: chrome-extension: //rest-console-id User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17 

enter image description here

Settings.py

REST_FRAMEWORK = {     'DEFAULT_PERMISSION_CLASSES': (         'rest_framework.authentication.TokenAuthentication',         'rest_framework.permissions.IsAdminUser',       ),     'PAGINATE_BY': 10 } 

view.py

class ProfileList(generics.ListCreateAPIView):     """     API endpoint that represents a list of users.     """     permission_classes = (permissions.IsAuthenticated,)     model = Profile     serializer_class = ProfileSerializer      def pre_save(self, obj):         obj.owner = self.request.user 
like image 467
Prometheus Avatar asked Feb 14 '13 14:02

Prometheus


People also ask

What is the best authentication for Django REST framework?

JSON Web Token (JWT) Authentication.

What is basic authentication in Django REST framework?

Authentication is a mechanism that provides access control based on the credentials associated with incoming requests. Django REST Framework provides several authentication schemes.


2 Answers

Just in case anyone else comes across this error. This can also happen if you are running Django on Apache using mod_wsgi because the authorization header is stripped out by mod_wsgi. You'll need to add the following to your VirtualHost configuration:

WSGIPassAuthorization On

like image 52
Fiver Avatar answered Sep 23 '22 01:09

Fiver


Assuming you're trying to use TokenAuthentication, the header should look like this:

Authorization: Token 6d82549b48a8b079f618ee9c51a6dfb59c7e2196 

As described in the documentation.

like image 31
Tom Christie Avatar answered Sep 20 '22 01:09

Tom Christie