[英]"detail": "Authentication credentials were not provided." When Creating a new user
I am attempting to create a new user in my platform when they sign up for my app.当他们注册我的应用程序时,我试图在我的平台中创建一个新用户。 I want them to enter their details and send it up to the server to create the account so they can sign in and then receive a token.
我希望他们输入他们的详细信息并将其发送到服务器以创建帐户,以便他们可以登录然后接收令牌。 Whenever I use postman to send up the credentials I get this error:
每当我使用邮递员发送凭据时,我都会收到此错误:
"detail": "Authentication credentials were not provided."
"detail": "未提供身份验证凭据。"
This is what I have so far:这是我到目前为止:
Settings.py :设置.py :
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'buddysapp',
'oauth2_provider',
'social_django',
'rest_framework_social_oauth2',
'bootstrap3',
'multiselectfield',
'openinghours',
'whitenoise.runserver_nostatic',
'import_export',
'phone_field',
'django_s3_storage',
'rest_framework',
'rest_auth',
'rest_framework.authtoken',
]
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_simplejwt.authentication.JWTAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.BasicAuthentication',
'oauth2_provider.contrib.rest_framework.OAuth2Authentication',
'rest_framework_social_oauth2.authentication.SocialAuthentication',
'rest_framework.authentication.TokenAuthentication',
'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
),
}
REST_USE_JWT = True
def jwt_get_username_from_payload_handler(user):
return {
'username': user.username,
'email': user.email
}
SIMPLE_JWT = {
'ACCESS_TOKEN_LIFETIME': datetime.timedelta(minutes=30),
'REFRESH_TOKEN_LIFETIME': datetime.timedelta(hours=1),
'ROTATE_REFRESH_TOKENS': False,
'BLACKLIST_AFTER_ROTATION': True,
'ALGORITHM': 'HS256',
'SIGNING_KEY': SECRET_KEY,
'VERIFYING_KEY': None,
'AUTH_HEADER_TYPES': ('Bearer',),
'USER_ID_FIELD': 'id',
'USER_ID_CLAIM': 'user_id',
'AUTH_TOKEN_CLASSES': ('rest_framework_simplejwt.tokens.AccessToken',),
# 'TOKEN_TYPE_CLAIM': 'token_type',
'SLIDING_TOKEN_REFRESH_EXP_CLAIM': 'refresh_exp',
'SLIDING_TOKEN_LIFETIME': datetime.timedelta(minutes=5),
'SLIDING_TOKEN_REFRESH_LIFETIME': datetime.timedelta(days=1),
}
Views.py视图.py
@api_view(['POST',])
def createApp_user(request):
if request.method == 'POST':
serializer = AppSignUpSerializer(data=request.data)
data = {}
if serializer.is_valid():
user=serializer.save()
data['response']= 'Successfully registered new user.'
data['email'] = user.email
data['username'] = user.username
# else:
# data = serializer.errors
return Response(data)
Serializers.py序列化器.py
class AppSignUpSerializer(serializers.ModelSerializer):
class Meta:
model = User
extra_kwargs = {'password': {'password': True}}
fields = ('id', 'username', 'email', 'password', 'first_name', 'last_name')
email = serializers.EmailField(
required=True,
validators=[UniqueValidator(queryset=User.objects.all())]
)
username = serializers.CharField(
max_length=32,
validators=[UniqueValidator(queryset=User.objects.all())]
)
password = serializers.CharField(min_length=6, max_length=100,
write_only=True)
def save(self):
user = User(
first_name=self.validated_data['first_name'],
last_name=self.validated_data['last_name'],
email=self.validated_data['email'],
username=self.validated_data['username']
)
password = self.validated_data['password']
user.set_password(password)
user.save()
return user
This is because your global DRF settings is applied to the createApp_user
view also.这是因为您的全局 DRF 设置也应用于
createApp_user
视图。
What you have to do is, provide empty permission_classes
and authentication_classes
settings to your view by corresponding decorators, as below您需要做的是,通过相应的装饰器为您的视图提供空的
permission_classes
和authentication_classes
设置,如下所示
from rest_framework.decorators import api_view, authentication_classes, permission_classes
@api_view(['POST', ])
@authentication_classes(()) @permission_classes(())
def createApp_user(request):
...
# your rest of the code
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.