DRF auth_token: "non_field_errors": [ "Unable to log in with provided credentials."
<p>Both JWT packages written for Django gave me issues with poor documentation, so I try DRF-auth_token package. This is a good example I followed, <a href="http://stackoverflow.com/questions/14838128/django-rest-framework-token-authentication">Django Rest Framework Token Authentication</a>. You should in theory be able to go to </p>

<p><code>localhost:8000/api-token-auth/</code></p>

<p>urls.py:</p>

<pre><code>from django.conf.urls import url, include
from django.contrib import admin
from django.contrib.auth.models import User
from rest_framework.authtoken import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^api/', include('api.urls', namespace='api')),
    url(r'^orders/', include('orders.urls', namespace='orders')),
    url(r'^api-token-auth/', views.obtain_auth_token, name='auth-token'),

]
</code></pre>

<p>Getting a token for users is not working so I have rewritten it myself to make it work:</p>

<pre><code>@api_view(['POST'])
def customer_login(request):
    """
    Try to login a customer (food orderer)
    """
    data = request.data

    try:
        username = data['username']
        password = data['password']
    except:
        return Response(status=status.HTTP_400_BAD_REQUEST)

    try:
        user = User.objects.get(username=username, password=password)
    except:
        return Response(status=status.HTTP_401_UNAUTHORIZED)

    try:
        user_token = user.auth_token.key
    except:
        user_token = Token.objects.create(user=user)

    data = {'token': user_token}
    return Response(data=data, status=status.HTTP_200_OK)
</code></pre>

<p>My version works:</p>

<pre><code>http://localhost:8000/api/login/customer-login/
{"username": "thisguy@example.com", "password": "wombat"}
--&gt;
{
  "token": "292192b101153b7ced74dd52deb6b3df22ef2c74"
}
</code></pre>

<p>The DRF auth_token does not work:</p>

<pre><code>http://localhost:8000/api-token-auth/
{"username": "thisguy@example.com", "password": "wombat"}
--&gt;
{
  "non_field_errors": [
    "Unable to log in with provided credentials."
  ]
}
</code></pre>

<p>settings.py</p>

<pre><code>INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # third party:
    'django_extensions',
    'rest_framework',
    'rest_framework.authtoken',



REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
    ),
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',
    )
}
</code></pre>

<p>It seems set up correctly. Every user in my DB has a token. Each user is <code>is_authenticated</code> and <code>is_active</code> in DB. Super users can get their token:</p>

<pre><code>localhost:8000/api-token-auth/
{"username": "mysuperuser", "password": "superuserpassword"}
--&gt;
{
  "token": "9297ff1f44dbc6caea67bea534f6f7590d2161b0"
}
</code></pre>

<p>for some reason, only super user can get a token:</p>

<pre><code>localhost:8000/api-token-auth/
{"username": "regularguy", "password": "password"}
--&gt;
{
  "non_field_errors": [
    "Unable to log in with provided credentials."
  ]
}
</code></pre>

<p>Why can't my users log in and get their token? Thank you</p>

<p>I went ahead and did this from the <a href="http://www.django-rest-framework.org/api-guide/authentication/#tokenauthentication" rel="nofollow">drf token auth docs</a> and didn't run into any problems with superusers, staffusers, or normal users. Maybe take a look at my code and see if you can find some difference. <a href="https://github.com/awwester/so40076254drftoken" rel="nofollow">https://github.com/awwester/so40076254drftoken</a></p>

<p>Also try following the steps of the official docs instead of that SO answer and see if that fixes the problem - it's possible something changed.</p>

<p>Here were the general steps I took:</p>

<ul>
<li>install django, drf</li>
<li>put 'rest_framework' and 'rest_framework.authtoken' in INSTALLED_APPS</li>
<li>add 'TokenAuthentication' in my rest_framework settings</li>
<li>run migrate</li>
<li>create tokens for users (I just did this in urls.py)</li>
<li>create the url for token</li>
<li>POST <a href="http://localhost:8000/token/" rel="nofollow">http://localhost:8000/token/</a> {"username": "...", "password": "..."}</li>
</ul>

<p>If you have the code public anywhere I'd be glad to take a further look and see what I find.</p>

python django django-rest-framework django-rest-auth django-1.10