当他们注册我的应用程序时,我正在尝试在平台上创建一个新用户。我希望他们输入详细信息并将其发送给服务器以创建帐户,以便他们可以登录然后接收令牌。每当我使用邮递员发送凭据时,都会出现此错误:
“详细信息”:“未提供身份验证凭据。”
这是我到目前为止所拥有的:
Settings.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
@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
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
答案 0 :(得分:0)
这是因为您的 全局DRF设置也已应用到createApp_user
视图。
您要做的是,由相应的装饰器为您的视图提供空的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