使用django oauth工具包的django rest框架

时间:2014-12-07 01:11:13

标签: django django-rest-framework

我正在尝试为我的api设置身份验证。我是API的新手,但希望制作更多基于客户端的网络应用。我想解决的第一个问题是客户端身份验证。我转到http://127.0.0.1:8000/o/applications/(我认为这就是我需要去的地方),但它会将我重定向到http://127.0.0.1:8000/accounts/login/?next=/o/applications/未找到的页面

继承我的代码:

Settings.py:

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'oauth2_provider.ext.rest_framework.OAuth2Authentication',
    ),
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
    ),
    'DEFAULT_RENDERER_CLASSES': (
        'rest_framework.renderers.JSONRenderer',
    ),
    'DEFAULT_PARSER_CLASSES': (
        'rest_framework.parsers.JSONParser',

urls.py:

 url(r'^api/users/$', api.UserList.as_view()),
                       url(r'^api/users/(?P<pk>[0-9+]+)/$', api.UserDetail.as_view()),

                       url(r'^o/', include('oauth2_provider.urls', namespace='oauth2_provider')),

api.py

from django.contrib.auth.models import User
from django.http import HttpResponse

from rest_framework import generics
from oauth2_provider.views.generic import ProtectedResourceView

from serializers import UserSerializer


class APIEndpoint(ProtectedResourceView):
    def get(self, request, *args, **kwargs):
        return HttpResponse('Protected with OAuth2!')


class UserList(generics.ListCreateAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer


class UserDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer

1 个答案:

答案 0 :(得分:0)

在这里找到答案https://github.com/evonove/django-oauth-toolkit/issues/121我需要在我的网址中使用登录模板