"detail":"Authentication credentials were not provided."提出uid和令牌时收到此错误



我创建了基于类的视图来访问uid和令牌。

我想激活创建的用户。我通过电子邮件收到激活链接。当我点击链接,我希望激活该用户。但是在响应中得到错误401并且{"detail":"未提供身份验证凭据。内容中的"}

我也尝试创建基于函数的视图,但给出相同的错误

基于类的视图:

class UserActivationView(APIView):
def get (request , uid , token ): 
protocol = 'https://' if request.is_secure() else 'http://'
web_url = protocol + request.get_host()
post_url = web_url + "/auth/users/activate/"
post_data = {'uid': uid, 'token': token}
result = requests.post(post_url, data = post_data)
content = result.text
return Response(content)
<<p>基于函数的视图/strong>
@api_view(["GET"])
@permission_classes([permissions.AllowAny])
def get (request , uid , token ): 
protocol = 'https://' if request.is_secure() else 'http://'
web_url = protocol + request.get_host()
post_url = web_url + "/auth/users/activate/"
print("post_url :", post_url)
post_data = {'uid': uid, 'token': token}
print("post_data : ", post_data)
result = requests.post(post_url, data = post_data)
print("result : ",result)
content = result.text
print("content : ", content)
return Response(content)

urls . py

urlpatterns = [
# re_path(r'^auth/users/activate/(?P<uid>[w-]+)/(?P<token>[w-]+)/$', get),
re_path(r'^auth/users/activate/(?P<uid>[w-]+)/(?P<token>[w-]+)/$', UserActivationView.as_view()),
]

settings.py

DJOSER = {
'ACTIVATION_URL': "auth/users/activate/" + '{uid}/{token}/',
'SEND_ACTIVATION_EMAIL': True,
'SEND_CONFIRMATION_EMAIL': True,
'PASSWORD_CHANGED_EMAIL_CONFIRMATION': True,
'USERNAME_CHANGED_EMAIL_CONFIRMATION': True,
'USER_CREATE_PASSWORD_RETYPE': True, #Designed to propote good programming practice
'SET_PASSWORD_RETYPE': True, #Designed to propote good programming practice
'PASSWORD_RESET_CONFIRM_RETYPE': True, #Designed to propote good programming practice
'LOGOUT_ON_PASSWORD_CHANGE' : True, #Note : Logout only works with token based authentication. djoser 2.10
'PASSWORD_RESET_SHOW_EMAIL_NOT_FOUND': False, #Please note that setting this to True will expose information whether an email is registered in the system
'USERNAME_RESET_SHOW_EMAIL_NOT_FOUND': False, #Please note that setting this to True will expose information whether an email is registered in the system
'HIDE_USERS': True,
'token': 'djoser.serializers.TokenSerializer',
'token_create': 'djoser.serializers.TokenCreateSerializer',
'LOGIN_FIELD': 'email', #Default: User.USERNAME_FIELD where User is the model set with Django’s setting AUTH_USER_MODEL.
'SERIALIZERS': {
'user': 'user_profile.serializer.UserSerializer',
},
}

urls . py

path("", include('app_user.urls')),
re_path(r'^auth/', include('djoser.urls')),
re_path(r'^auth/', include('djoser.urls.authtoken')),#For using django token

要发出此请求,您需要通过身份验证。尝试在您的授权头中添加令牌。

相关内容

  • 没有找到相关文章

最新更新