简体   繁体   中英

Rewriting DRF token auth

I wanted to allow a user to have multiple tokens so decided to rewrite the Token model. As result created

class TokenAuthentication(rest_framework.authentication.TokenAuthentication):
    model = Token

In my settings added to REST_FRAMEWORK

'DEFAULT_AUTHENTICATION_CLASSES': (
    'users.authentication.TokenAuthentication',
)

Also modified

class ObtainAuthToken(APIView):
    authentication_classes = ()
    throttle_classes = ()
    permission_classes = ()
    parser_classes = (parsers.FormParser, parsers.MultiPartParser, parsers.JSONParser,)
    renderer_classes = (renderers.JSONRenderer,)
    serializer_class = AuthTokenSerializer


    def post(self, request, *args, **kwargs):
        serializer = self.serializer_class(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = serializer.validated_data['user']
        name = serializer.validated_data['name']
        token, created = Token.objects.get_or_create(user=user, name=name)
        return Response({'token': token.key})


obtain_auth_token = ObtainAuthToken.as_view()

and AuthTokenSerializer to return the name

Finaly in the urls got

url(r'^token-auth/', obtain_auth_token),

I think all is correct but keep getting the errors

 File "/home/me/code/python/OCManager/core/users/authentication.py", line 4, in <module>
    from rest_framework.views import APIView
ImportError: cannot import name 'APIView'

and

ImportError: Could not import 'users.authentication.TokenAuthentication' for API setting 'DEFAULT_AUTHENTICATION_CLASSES'. ImportError: cannot import name 'APIView'.

Any hint what it could be?

The Token class modification is this:

class Token(rest_framework.authtoken.models.Token):
    # key is no longer primary key, but still indexed and unique
    key = models.CharField(_("Key"), max_length=40, db_index=True, unique=True)
    # relation to user is a ForeignKey, so each user can have more than one token
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL, related_name='auth_tokens',
        on_delete=models.CASCADE, verbose_name=_("User")
    )
    name = models.CharField(_("Name"), max_length=64)

    class Meta:
        unique_together = (('user', 'name'),)

    def __str__(self):
        return self.user.username + " - " + self.name

I managed to find what was wrong. Having the TokenAuth extension in the same file was causing some import error, seems trying to import itself in other file. The solution was moving

class TokenAuthentication(rest_framework.authentication.TokenAuthentication):
    model = Token

to another file

请确保你已经导入TokenAuthenticationrest_framework.authentication ,而不是从rest_framework.authToken

I was dealing with this error FOREVER. It turned out to have to do with tokens like everyone else was saying. This isn't exactly a "fix" but it got rid of the error for me:

I was using simplejwt and commented out the import: from rest_framework_simplejwt.tokens import RefreshToken And also commented out the function using the import.

My error was resolved after this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM