I'm going to restrict my working rest_framework.views.APIView
inherited class, to be visible only by authenticated users.
I made these modifications:
- Added
authentication_classes
andpermission_classes
to my class:
class TestView(APIView):
authentication_classes = (OAuth2Authentication,)
permission_classes = (IsAuthenticated,)
return Response("Hey there...")
- Got an access_token from django-oauth-toolkit:
curl -XPOST "http://172.18.0.1:8000/oauth2/token/" \
-d 'grant_type=client_credentials&client_id=MY-CLIENT-ID&client_secret=MY-CLIENT-SECRET'
{"access_token": "Haje1ZTrc7VH4rRkTbJCIkX5u83Tm6", "expires_in": 36000, "token_type": "Bearer", "scope": "read write groups"}
- Tried requesting the view without setting the
access_token
:
curl -XGET "http://172.18.0.1:8000/api/v1/test/"
{"detail":"Authentication credentials were not provided."}
- Tried a new request with the
access_token
:
curl -XGET "http://172.18.0.1:8000/api/v1/test/" \
-H "Authorization: Bearer Haje1ZTrc7VH4rRkTbJCIkX5u83Tm6"
{"detail":"You do not have permission to perform this action."}
Should I do anything more to enable access_token authentication?
Note: I have installed these libraries in the environment:
Django==2.2.17
djangorestframework==3.12.2
django-oauth-toolkit==1.3.3
Note2: Forgot to say that rest_framework
and oauth2_provider
have been added to INSTALLED_APPS.
IMPORTANT EDIT:
This problem exists only if using "client credential grant" for authentication. check my own answer below.