" AccessToken.user"必须是"用户"例

时间:2016-09-07 07:00:16

标签: python django python-3.x oauth django-rest-framework

我试图在3天内找出基于oAuth2令牌的身份验证,但尚无效。我已经编写了用于用户身份验证的代码。但是,我收到错误

  

/ api / customer / login /上的ValueError无法分配" {'用户名':   ' sanskars'}":" AccessToken.user"必须是"用户"实例

这可能是什么灵魂?为什么会出现以下错误?

views.py

class UserLoginAPI(APIView):
    permission_classes = [AllowAny]
    serializer_class = UserLoginSerializer

    def post(self, request, *args, **kwargs):
        access_token = request.GET.get('access_token')
        data = request.data
        serializer = UserLoginSerializer(data=data)
        if serializer.is_valid(raise_exception=True):
            new_data = serializer.data
            if new_data:
                app = Application.objects.get(name="appname")
                try:
                    print('trying to fetch old token if exists')
                    old_token = AccessToken.objects.get(user=new_data, appliction=app)
                    print('old_token',old_token)
                except:
                    pass
                else:
                    old_token.delete()
                new_token = generate_token()
                print('new_token',new_token)
                AccessToken.objects.create(user=new_data, application=app, expires=datetime.now() + timedelta(days=365),token=new_token)
                print('aceess',AccessToken)
            return Response(new_data, status=status.HTTP_200_OK)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) 

serializers.py

class UserLoginSerializer(ModelSerializer):
    username = CharField()
    # email = EmailField(label='Email Address')
    class Meta:
        model = User
        fields = [
            'username',
            'password',

        ]
        extra_kwargs = {"password":{"write_only": True} }

另外一个困惑是我需要在注册用户时生成令牌吗?

0 个答案:

没有答案