我尝试实现Django Oauth,以便使用基于类的方法生成用户创建的Access令牌。
serializer.py
class UserCreateSerializer(ModelSerializer):
def create(self, validated_data):
user = User.objects.create_user(validated_data['username'],
validated_data['email'],
validated_data['password'])
return user
class Meta:
model = User
fields = ('username', 'email' ,'password')
views.py
class User_Create_view(CreateAPIView):
serializer_class = UserCreateSerializer
queryset = User.objects.all()
permission_classes = [AllowAny]
authentication_classes = Has_Access_Token
def create(self, request):
serializers =self.serializer_class(data=request.data)
if serializers.is_valid():
# pdb.set_trace()
serializers.save()
#Has_Access_Token.access_token()
return Response(serializers.data)
permission.py
class Has_Access_Token(BaseAuthentication):
def access_token(request):
app = Application.objects.get(name="testing")
tok = generate_token()
acce_token=AccessToken.objects.create(
user=User.objects.all().last(),
application=app,
expires=datetime.datetime.now() + datetime.timedelta(days=365),
token=tok)
return acce_token
# @method_decorator(access_token)
def authenticate(self):
return request
如果我使用上面的隐式方法,就创建用户而不是最佳实践生成访问令牌。因此,为了在APIView中创建自定义身份验证类,标准过程是什么,无论是关于装饰器还是通过BaseAuthentication类,我应该更改什么。
当我不使用装饰器时
File "/home/allwin/Desktop/response/env/local/lib/python2.7/site-packages/rest_framework/views.py", line 262, in get_authenticators
return [auth() for auth in self.authentication_classes]
TypeError: 'type' object is not iterable
当我使用装饰器
时File "/usr/lib/python2.7/functools.py", line 33, in update_wrapper
setattr(wrapper, attr, getattr(wrapped, attr))
AttributeError: 'tuple' object has no attribute '__module__'
如何调试上述问题,解决装饰器问题还是改变类变量?