AccessToken matching query does not exist
<p>I am creating a an API for registering user based on oauth token. My app has functionality like registration and login, adding restaurant etc. I created user registration part but i get error while login. I want login based on token. </p>

<p>I have used django-oauth-toolkit for this and DRF. </p>

<p>What i have done is</p>

<p><strong>Login based on token</strong></p>

<pre><code>class UserCreateAPI(CreateAPIView):
    serializer_class = UserCreateSerializer
    queryset = User.objects.all()
    permission_classes = [AllowAny]

class UserLoginAPI(APIView):
    permission_classes = [AllowAny]
    serializer_class = UserLoginSerializer

    def post(self, request, *args, **kwargs):
        access_token = AccessToken.objects.get(token=request.POST.get('access_token'), expires__gt=timezone.now()) # error is shown here
        data = request.data
        serializer = UserLoginSerializer(data=data)
        if serializer.is_valid(raise_exception=True):
            new_data = serializer.data
            return Response(new_data, status=status.HTTP_200_OK)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
</code></pre>

<p><strong>For creating user with a token</strong></p>

<pre><code>class UserCreateSerializer(ModelSerializer):
    class Meta:
        model = User
        extra_kwargs = {"password": {"write_only": True}}

    def create(self, validated_data):
        username = validated_data['username']
        first_name = validated_data['first_name']
        last_name = validated_data['last_name']
        email = validated_data['email']
        password = validated_data['password']
        confirm_password = validated_data['password']
        user_obj = User(
                username = username,
                first_name = first_name,
                last_name = last_name,
                email = email
            )
        user_obj.set_password(password)
        user_obj.save()
        if user_obj:
            expire_seconds = oauth2_settings.user_settings['ACCESS_TOKEN_EXPIRE_SECONDS']
            scopes = oauth2_settings.user_settings['SCOPES']

            application = Application.objects.get(name="Foodie")
            expires = datetime.now() + timedelta(seconds=expire_seconds)
            access_token = AccessToken.objects.create(user=user_obj, 
                                                    application=application,
                                                    token = generate_token(),
                                                    expires=expires, 
                                                    scope=scopes)
        return validated_data


class UserLoginSerializer(ModelSerializer):
    # token = CharField(allow_blank=True, read_only=True)
    username = CharField()
    class Meta:
        model = User
        fields = [
            'username',
            'password',
            # 'token',

        ]
        extra_kwargs = {"password":
                            {"write_only": True}
                            }
</code></pre>

<p>I have excluded fields part and validation part for shortening the code</p>

<p>Am i doing the right way?</p>

<p>Try to use request.data instead of request.post in order to get the access_token(<a href="http://www.django-rest-framework.org/tutorial/2-requests-and-responses/" rel="nofollow">http://www.django-rest-framework.org/tutorial/2-requests-and-responses/</a>):</p>

<pre><code>access_token = AccessToken.objects.get(token=request.data.get('access_token'), expires__gt=timezone.now())
</code></pre>

<p>use request.DATA if you are using the Version-2 of DRF. request.data is for version-3</p>

<p>============UPDATE========================================
You should implement your own login procedure or modify the existing one; because when user log in, the access_token is not actually sent to the server.</p>

<p>The login procedure should look like this :</p>

<ol>
<li><p>When the user enter his login and password, your app should send a post request to 127.0.0.1:8000/o/token asking for the token. The request should contains the username, password, client_id and client_secret.</p></li>
<li><p>the server then receives the credentials and if they are valid it returns the access_token.</p></li>
<li><p>The rest of the time you should query the server using that token. </p></li>
</ol>

<p>But, i'm not sure that it is what you are actually doing.</p>

python django python-3.x oauth django-rest-framework