"detail": "未提供身份验证凭据。"设置 uid 和 token 时出现此错误

Posted

技术标签:

【中文标题】"detail": "未提供身份验证凭据。"设置 uid 和 token 时出现此错误【英文标题】:"detail": "Authentication credentials were not provided." getting this error while posing uid and token 【发布时间】:2021-11-02 08:59:32 【问题描述】:

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

我希望激活创建的用户。我正在通过电子邮件收到激活链接。当我单击链接时,我希望激活该用户。但是在响应中收到错误 401 和 "detail":"Authentication credentials were not provided." in content

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

基于类的视图:

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)

基于函数的视图

@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

【问题讨论】:

【参考方案1】:

要发出此请求,您需要经过身份验证。尝试将令牌添加到您的授权标头。

【讨论】:

我将令牌添加为身份验证标头,但仍然给我同样的错误。 headers='Authorization': token result = requests.post(post_url, headers=headers ,data = post_data) 添加了这两行用于添加身份验证标头。

以上是关于"detail": "未提供身份验证凭据。"设置 uid 和 token 时出现此错误的主要内容,如果未能解决你的问题,请参考以下文章

"detail": "Method \"GET\" not allowed. 在 django 中调用端点

"errors":"errors":["detail":"您无权执行此操作。","code":&qu

"detail": "未提供身份验证凭据。"创建新用户时

模型 \"Trains\ 的路径 \"setno\" 处的值 \" details: '233' \" 转换为字符串失败

为啥响应总是 "detail":"Unsupported media type \"text/plain\" in request." 在

"detail": "未提供身份验证凭据。"设置 uid 和 token 时出现此错误