gpt4 book ai didi

python - DRF auth_token : "non_field_errors": [ "Unable to log in with provided credentials."

转载 作者:太空宇宙 更新时间:2023-11-04 07:16:58 29 4
gpt4 key购买 nike

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

localhost:8000/api-token-auth/

urls.py:

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

]

为用户获取 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": "thisguy@example.com", "password": "wombat"}
-->
{
"token": "292192b101153b7ced74dd52deb6b3df22ef2c74"
}

DRF auth_token 不起作用:

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

设置.py

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

看起来设置正确。我数据库中的每个用户都有一个 token 。每个用户在数据库中都是is_authenticatedis_active。 super 用户可以获得他们的 token :

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

出于某种原因,只有 super 用户才能获得 token :

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

为什么我的用户无法登录并获取他们的 token ?谢谢

最佳答案

  • 我在尝试使用此 API 端点时遇到了相同的错误消息:
    来自 rest_framework.authtoken.views 的“obtain_auth_token”类,
    但是惊喜!问题是 User serializer 在第一名 ! .

  • 用户是使用 API endppint 创建的,他们的密码被保存为纯文本!,如以下屏幕截图所示: User Database
    但是 TokenAPI 端点加密了密码,所以从那里来了冲突! ,

  • 我已经更改了 User Serializer 类 并覆盖了 create 函数以使用对密码进行哈希处理的 set_password 函数:

    class UserSerializer(serializers.ModelSerializer):
    class Meta:
    model = User
    fields = ['email', 'username', 'password']
    extra_kwargs = {'password': {'write_only': True}}

    def create(self, validated_data):
    user = User(
    email=validated_data['email'],
    username=validated_data['username']
    )
    user.set_password(validated_data['password'])
    user.save()
    return user
  • 现在我已经编辑了我的用户序列化器,数据存储如下: User database after modification

  • 因此错误:“non_field_errors”: [“无法使用提供的凭据登录。” 停止显示! ,并且 token API 端点“localhost:8000/api-token-auth/”有效!

关于python - DRF auth_token : "non_field_errors": [ "Unable to log in with provided credentials.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40076254/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com