Zrobiłem klasę uwierzytelniania po prostu tak:obiekt 'WSGIRequest' nie ma atrybutu 'successful_authenticator'
Token Authentication for RESTful API: should the token be periodically changed?
restapi/settings.py
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
# 'rest_framework.authentication.TokenAuthentication',
'restapi.authentication.ExpiringTokenAuthentication',
),
'PAGINATE_BY': 10
}
restapi/authentication.py
import datetime
from rest_framework.authentication import TokenAuthentication
class ExpiringTokenAuthentication(TokenAuthentication):
def authenticate_credentials(self, key):
try:
token = self.model.objects.get(key=key)
except self.model.DoesNotExist:
raise exceptions.AuthenticationFailed('Invalid token')
if not token.user.is_active:
raise exceptions.AuthenticationFailed('User inactive or deleted')
# This is required for the time comparison
utc_now = datetime.utcnow()
utc_now = utc_now.replace(tzinfo=pytz.utc)
if token.created < utc_now - timedelta(hours=24):
raise exceptions.AuthenticationFailed('Token has expired')
return token.user, token
restapi/tests.py
def test_get_missions(self):
"""
Tests that /missions/ returns with no error
"""
response = self.client.get('/missions/', HTTP_AUTHORIZATION = self.auth)
W moich testach, mam wyjątek AttributeError: 'WSGIRequest' object has no attribute 'successful_authenticator'
Dlaczego mam ten błąd? Jak to naprawić?
powinno być '' nie self.request' wezwania do? – karthikr
Nie jest to niezwiązane. –
co masz na myśli mówiąc, że nie jest to powiązane? – karthikr