Django (using TokenAuthentication): “non_field_errors”: "Unable to log in with provided credentials?

前端 未结 2 1906
萌比男神i
萌比男神i 2021-01-21 05:17

I\'m using httpie to test my custom authentication.

http POST http://127.0.0.1:8000/api-token-auth/ username=\'username1\' password=\'Password123\'
2条回答
  •  后悔当初
    2021-01-21 05:49

    If you search for the error string in the DRF code, you find this (in authtoken/serializers.py:

    from django.contrib.auth import authenticate
    ...
    
    if username and password:                                                                                                                                                                                                              
        user = authenticate(username=username, password=password)                                                                                                                                                                          
    
        if user:                                                                                                                                                                                                                           
            # From Django 1.10 onwards the `authenticate` call simply                                                                                                                                                                      
            # returns `None` for is_active=False users.                                                                                                                                                                                    
            # (Assuming the default `ModelBackend` authentication backend.)                                                                                                                                                                
            if not user.is_active:                                                                                                                                                                                                         
                msg = _('User account is disabled.')                                                                                                                                                                                       
                raise serializers.ValidationError(msg, code='authorization')
        else:                                                                                                                                                                                                                     
            msg = _('Unable to log in with provided credentials.')                                                                                                                                                                         
            raise serializers.ValidationError(msg, code='authorization')
    ...
    

    So it looks like depending on which version of Django you're using, either these credentials are incorrect, or the user is not active (for Django >= 1.10)?

    Have you tried logging in manually in the admin with these credentials to verify them?

提交回复
热议问题