Using Django OAuth toolkit on custom views












0















I've been using the Django OAuth toolkit to restrict access to my API. The problem is I'm not sure how to use it for my own views. It has it's own built-in "Application" views that I can see it works for but not sure how it can work for mine.



I tried using their generic classes (https://django-oauth-toolkit.readthedocs.io/en/latest/views/class_based.html) but to no avail:



views.py:



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


This gives me a AttributeError: type object 'PostCarFax' has no attribute 'get_extra_actions' error



I wonder if it's the way it defines it's URLs? From their code:



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"),
]


I guess I could define my own views there? Here's my 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')),
]









share|improve this question



























    0















    I've been using the Django OAuth toolkit to restrict access to my API. The problem is I'm not sure how to use it for my own views. It has it's own built-in "Application" views that I can see it works for but not sure how it can work for mine.



    I tried using their generic classes (https://django-oauth-toolkit.readthedocs.io/en/latest/views/class_based.html) but to no avail:



    views.py:



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


    This gives me a AttributeError: type object 'PostCarFax' has no attribute 'get_extra_actions' error



    I wonder if it's the way it defines it's URLs? From their code:



    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"),
    ]


    I guess I could define my own views there? Here's my 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')),
    ]









    share|improve this question

























      0












      0








      0








      I've been using the Django OAuth toolkit to restrict access to my API. The problem is I'm not sure how to use it for my own views. It has it's own built-in "Application" views that I can see it works for but not sure how it can work for mine.



      I tried using their generic classes (https://django-oauth-toolkit.readthedocs.io/en/latest/views/class_based.html) but to no avail:



      views.py:



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


      This gives me a AttributeError: type object 'PostCarFax' has no attribute 'get_extra_actions' error



      I wonder if it's the way it defines it's URLs? From their code:



      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"),
      ]


      I guess I could define my own views there? Here's my 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')),
      ]









      share|improve this question














      I've been using the Django OAuth toolkit to restrict access to my API. The problem is I'm not sure how to use it for my own views. It has it's own built-in "Application" views that I can see it works for but not sure how it can work for mine.



      I tried using their generic classes (https://django-oauth-toolkit.readthedocs.io/en/latest/views/class_based.html) but to no avail:



      views.py:



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


      This gives me a AttributeError: type object 'PostCarFax' has no attribute 'get_extra_actions' error



      I wonder if it's the way it defines it's URLs? From their code:



      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"),
      ]


      I guess I could define my own views there? Here's my 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')),
      ]






      python django oauth






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 '18 at 21:42









      Charles SmithCharles Smith

      6471918




      6471918
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270522%2fusing-django-oauth-toolkit-on-custom-views%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270522%2fusing-django-oauth-toolkit-on-custom-views%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Full-time equivalent

          Bicuculline

          さくらももこ