Thursday, April 8, 2021

Re: reg: DRF login API

Refer the docs


On Thu, Apr 8, 2021 at 6:44 PM RANGA BHARATH JINKA <bharathjinka09@gmail.com> wrote:
Hi, You have to specify the POST method in the command
http -a USERNAME POST https://api.github.com/repos/httpie/httpie/issues/83/comments body='HTTPie is awesome! :heart:' 
All the best

On Thu, Apr 8, 2021 at 6:18 PM 'Amitesh Sahay' via Django users <django-users@googlegroups.com> wrote:
HI,

I have created a simple login API through django rest framework. Below is the code snippet:

from django.contrib.auth import login
from rest_framework.generics import ListCreateAPIView
from rest_framework import status
from rest_framework.response import Response
from rest_framework.authtoken.serializers import AuthTokenSerializer
from rest_framework.permissions import IsAuthenticated, IsAdminUser, AllowAny
from knox.views import LoginView as KLView
from knox.models import AuthToken
from .serializers import RegistrationSerializers
from .models import NewEmployeeProfile

class loginAPIView(KLView):
permission_classes = [AllowAny]

def post(self, request, format=None):
serializer = AuthTokenSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
user = serializer.validated_data['user']
login(request, user)
return super(loginAPIView, self).post(request, format=None)
 
URLS.py
from django.urls import path
from knox import views as knox_views
from .views import UserRegisterView, loginAPIView


urlpatterns = [
    path('register/', UserRegisterView.as_view(), name='register'),
    path('login/', loginAPIView.as_view(), name='login'),
    ]


Now when I do a POST request in postman with the email and the password. It throws error:

{
    "detail": "Invalid token."
}

Note:: Since I am using knox to generate token. So, when I do a new registration I get token as well. See sample below:

{
    "status": "OK",
    "message": {
        "email": "test.test@test.com",
        "first_name": "est",
        "last_name": "Sah",
        "employee_code": "6124368",
        "contact": "7500078619",
        "dob": null
    },
    "token": "db058f23ecc70f4fa3de4ac69a04dc48bb7579a63aea1ad3d038ce59b1511890"

I tried both, password and token to authenticate, but I am getting the same error. In the cmd prompt where the dev server is running, I am seeing below message

========================
[08/Apr/2021 15:26:33] "POST /apii/login/ HTTP/1.1" 403 27
Forbidden: /apii/login/
========================
In the postman raw body, I am inserting below json data

{
    "email": "test.test@test.com",
    "password": "db058f23ecc70f4fa3de4ac69a04dc48bb7579a63aea1ad3d038ce59b1511890"
}


Below is the settings.py content for DRF

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        #'rest_framework.authentication.TokenAuthentication',
        'rest_framework.authentication.SessionAuthentication',
        'knox.auth.TokenAuthentication',
    ],
}



httpie output

python.exe -m httpie 127.0.0.1:8000/apii/login/ 'Authorization: Token db058f23ecc70f4fa3de4ac69a04dc48bb7579a63aea1ad3d038ce59b1511890'

HTTP/1.1 405 Method Not Allowed
Allow: POST, OPTIONS
Content-Length: 40
Content-Type: application/json
Date: Thu, 08 Apr 2021 11:52:31 GMT
Referrer-Policy: same-origin
Server: WSGIServer/0.2 CPython/3.6.8
Vary: Accept, Cookie
X-Content-Type-Options: nosniff
X-Frame-Options: DENY

{
    "detail": "Method \"GET\" not allowed."
}


urlpatterns = [ path('register/', UserRegisterView.as_view(), name='register'), path('login/', loginAPIView.as_view(), name='login'), ]
Not sure if I am doing it the right way, please suggest. Thank you....

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2028966691.640551.1617886057684%40mail.yahoo.com.


--
Thanks and Regards

J. Ranga Bharath
cell: 9110334114


--
Thanks and Regards

J. Ranga Bharath
cell: 9110334114

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAK5m314UV5z%3D%3DOO-BhQFGZ9cMrnTo5ZMmYRtMmDOZsWfU7eBJA%40mail.gmail.com.

No comments:

Post a Comment