在自定义视图上使用Django OAuth工具包

时间:2018-11-12 21:42:31

标签: python django oauth

我一直在使用Django OAuth工具包来限制对我的API的访问。问题是我不确定如何在自己的视图中使用它。它具有自己的内置“应用程序”视图,我可以看到它的工作原理,但不确定它如何为我的工作。

我尝试使用其通用类(https://django-oauth-toolkit.readthedocs.io/en/latest/views/class_based.html),但无济于事:

views.py:

class PostCarFax(ReadWriteScopedResourceMixin, ProtectedResourceView):
    queryset = CarFax.objects.all()
    serializer_class = CarFaxSerializer

这给我一个AttributeError: type object 'PostCarFax' has no attribute 'get_extra_actions'错误

我想知道它是否是定义URL的方式吗?根据他们的代码:

urls.py:

from django.conf.urls import url

from . import views


app_name = "oauth2_provider"


base_urlpatterns = [
    url(r"^authorize/$", views.AuthorizationView.as_view(), name="authorize"),
    url(r"^token/$", views.TokenView.as_view(), name="token"),
    url(r"^revoke_token/$", views.RevokeTokenView.as_view(), name="revoke-token"),
    url(r"^introspect/$", views.IntrospectTokenView.as_view(), name="introspect"),
]


management_urlpatterns = [
    # Application management views
    url(r"^applications/$", views.ApplicationList.as_view(), name="list"),
    url(r"^applications/register/$", views.ApplicationRegistration.as_view(), name="register"),
    url(r"^applications/(?P<pk>[\w-]+)/$", views.ApplicationDetail.as_view(), name="detail"),
    url(r"^applications/(?P<pk>[\w-]+)/delete/$", views.ApplicationDelete.as_view(), name="delete"),
    url(r"^applications/(?P<pk>[\w-]+)/update/$", views.ApplicationUpdate.as_view(), name="update"),
    # Token management views
    url(r"^authorized_tokens/$", views.AuthorizedTokensListView.as_view(), name="authorized-token-list"),
    url(r"^authorized_tokens/(?P<pk>[\w-]+)/delete/$", views.AuthorizedTokenDeleteView.as_view(),
        name="authorized-token-delete"),
]

我想我可以在那里定义自己的观点?这是我的urls.py:

router = routers.DefaultRouter()
router.register(r'post_carfax', views.PostCarFax)
admin.autodiscover()

from rest_framework import generics, permissions, serializers

from oauth2_provider.contrib.rest_framework import TokenHasReadWriteScope, TokenHasScope


urlpatterns = [
    url(r'^api/v1/', include(router.urls)),
    url(r'^admin/', admin.site.urls),
    url('o/', include('oauth2_provider.urls', namespace='oauth2_provider')),
]

0 个答案:

没有答案