How do I specify DRF oauth required_scopes in function based api_view?

392 Views Asked by At

I'm trying to make my function based rest framework views which use the @api_view decorator with the Django OAuth Rest Framework Toolkit, where I'm using TokenHasScope as the permission_class. However, this needs a mandatory attribute called required_scopes but I'm not sure how I can get this specified in a function based views?

2

There are 2 best solutions below

0
Duilio On

Not exactly what you asked for, but an alternative to using Viewsets could be defining a view that subclass ScopedProtectedResourceView and set the scopes with the required_scopes attribute.

0
giwa_abdul On

You can create a view that subclasses TokenHasScope then hardcode get_scopes method to return the scopes you want. If the scopes change across different view, you can create a decorator which takes in a list of scopes and passes it on, then set your get_scopes method to pick up the scopes from that decorator.