1

I created class based view to access uid and token .

I wish to activate user that created. I am receiving the activation link via email. when I click on link I wish activate that user. But getting error 401 in response and {"detail":"Authentication credentials were not provided."} in content

Also I try by creating function- based view but giving same error

Class- Based view:

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)

Function-Based view

@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 Answers1

0

To make this request, you need to be authenticated. Try adding token to your authorization header.

Ashin Shakya
  • 690
  • 6
  • 9
  • I add token as authentication header but still giving me same error. `headers={'Authorization': token}` `result = requests.post(post_url, headers=headers ,data = post_data)` Added this two lines for add authentication header. – Shweta Shinde Sep 06 '21 at 09:10