DRF auth_token:“non_field_errors”:[“无法使用提供的凭据登录。”

问题描述 投票:0回答:3

为Django编写的两个JWT包都给我提供了文档很差的问题,所以我尝试了DRF-auth_token包。这是我遵循的一个很好的例子,Django Rest Framework Token Authentication。理论上你应该去

localhost:8000/api-token-auth/

URLs.朋友:

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'),

]

为用户获取令牌是行不通的,所以我自己重写了它以使其工作:

@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)

我的版本有效:

http://localhost:8000/api/login/customer-login/
{"username": "[email protected]", "password": "wombat"}
-->
{
  "token": "292192b101153b7ced74dd52deb6b3df22ef2c74"
}

DRF auth_token不起作用:

http://localhost:8000/api-token-auth/
{"username": "[email protected]", "password": "wombat"}
-->
{
  "non_field_errors": [
    "Unable to log in with provided credentials."
  ]
}

settings.朋友

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',
    )
}

它似乎设置正确。我的数据库中的每个用户都有一个令牌。每个用户都是DB中的is_authenticatedis_active。超级用户可以获得他们的令牌:

localhost:8000/api-token-auth/
{"username": "mysuperuser", "password": "superuserpassword"}
-->
{
  "token": "9297ff1f44dbc6caea67bea534f6f7590d2161b0"
}

出于某种原因,只有超级用户才能获得令牌:

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

为什么用户无法登录并获取令牌?谢谢

python django django-rest-framework django-rest-auth django-1.10
3个回答
4
投票

我继续从drf token auth docs做到这一点,并没有遇到超级用户,员工用户或普通用户的任何问题。

同时尝试按照官方文档的步骤而不是那个SO答案,看看是否能解决问题 - 可能会发生变化。

以下是我采取的一般步骤:

  • 安装django,drf
  • 把'rest_framework'和'rest_framework.authtoken'放在INSTALLED_APPS中
  • 在我的rest_framework设置中添加'TokenAuthentication'
  • 运行迁移
  • 为用户创建令牌(我刚刚在urls.py中执行此操作)
  • 为令牌创建url
  • POST http://localhost:8000/token/ {“username”:“...”,“password”:“...”}

如果您将代码公之于众,我很乐意进一步了解并查看我找到的内容。


1
投票

也许加密是罪魁祸首。我面临同样的问题。我比较了superuser的mysql和普通用户存储的信息(我们称之为user1)。我发现了一个区别。 superuser的密码已加密,但user1的密码未加密。所以我将user1的密码更改为superuser的密码,然后我posted user1的名称和密码到jwt api,我得到了正确的answer

现在我找到了答案,虽然它可能不是最好的答案,但应该有效。我刚刚在“ModelSerializer”中覆盖了“create”方法。步骤1:将“创建”方法从“ModelSerializer”复制到您自己的序列化文件步骤2:将句子“instance = ModelClass._default_manager.create(** validated_data)”更改为“instance = ModelClass._default_manager.create_user(** validated_data) “.enter image description here step3:有效] 4 [] 5


0
投票
  1. 检查用户名和密码
  2. 表users.is_active = 1中的字段
© www.soinside.com 2019 - 2024. All rights reserved.