Django Rest Framework: how to to authenticate with OAuth2 token using Custom User Model
I'm working with Django Rest Framework (DRF) 3.14 and trying to implement OAuth2 authentication using a custom user model. I've configured my application with `django-oauth-toolkit`, but I'm hitting a wall when trying to authenticate requests with an OAuth2 access token. The token is generated successfully, but when I make requests to protected endpoints, I'm receiving a `401 Unauthorized` response. I've set up my custom user model like this: ```python from django.contrib.auth.models import AbstractUser class CustomUser(AbstractUser): # Add any additional fields here pass ``` In my settings.py, I've added the custom user model: ```python AUTH_USER_MODEL = 'myapp.CustomUser' ``` I'm also using the following configuration in `settings.py` for OAuth2: ```python REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'oauth2_provider.contrib.rest_framework.OAuth2Authentication', ), } ``` My view is protected using the `@permission_classes` decorator: ```python from rest_framework.permissions import IsAuthenticated from rest_framework.views import APIView class ProtectedView(APIView): permission_classes = [IsAuthenticated] def get(self, request): return Response({'message': 'You have access!'}, status=200) ``` When I try to access this endpoint with the token using a header like this: ``` Authorization: Bearer YOUR_ACCESS_TOKEN ``` I'm still getting a `401 Unauthorized` behavior. I've confirmed that the token is valid and not expired using the token introspection endpoint. I've tried debugging through DRF's authentication layers but need to pinpoint where the failure is occurring. Could the scenario be related to the custom user model? I've also checked that all necessary migrations have been applied. Any insights or suggestions would be much appreciated! What am I doing wrong?