Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Rest Framework invalid username/password

Trying to do a simple 'GET' wih admin credentials returns

"detail": "Invalid username/password."

I have a custom user model where I deleted the username, instead I use facebook_id :

USERNAME_FIELD = 'facebook_id'

I tried changing the DEFAULT_PERMISSION_CLASSES:

('rest_framework.permissions.IsAuthenticated',), -- doesn't work!
('rest_framework.permissions.IsAdminUser',), -- doesn't work!

The only one that works is:

('rest_framework.permissions.AllowAny',),

But I do not want that, since I'm building an API for a Mobile App

I also declared a CustomUserAdmin model and CustomUserCreationForm , apparently this was not the problem


Help me understand what needs to be done to fix this annoying problem, I'm guessing it might have something to do with Permissions/Authentication or the fact that I CustomUserModel..

Also, let me know if there is a better way for a mobile app client to authenticate to the api

like image 612
Admiral Giggles Avatar asked Oct 17 '16 20:10

Admiral Giggles


1 Answers

Have just had the same problem. In my case the source of the problem was Apache's Basic Authentication, my browser was sending Authorization header and Django REST Framework thought that this header was to be handled by it. The solution is pretty simple: just remove 'rest_framework.authentication.BasicAuthentication' from your

REST_FRAMEWORK = {
    "DEFAULT_AUTHENTICATION_CLASSES": [ 
        # ... auth classes here ... 
    ]
}

Or explicitly set the default DEFAULT_AUTHENTICATION_CLASSES to remove BasicAuth from DRF's defaults.

REST_FRAMEWORK = {
    "DEFAULT_AUTHENTICATION_CLASSES": (
        "rest_framework.authentication.SessionAuthentication",
    ),
}
like image 50
Михаил Кормановский Avatar answered Sep 28 '22 03:09

Михаил Кормановский