【发布时间】:2020-10-22 15:53:28
【问题描述】:
我使用 Django Rest Framework 构建了一个简单的 api 端点,为了查看端点的数据,用户需要输入一个公钥和一个私钥。这是我所做的:
class CustomAuthentication(authentication.BaseAuthentication):
def authenticate(self, request):
# Get the username and password
public = request.data.get('public', None)
secret = request.data.get('secret', None)
if not public or not secret:
raise exceptions.AuthenticationFailed(_('No credentials provided.'))
credentials = {
get_user_model().USERNAME_FIELD: public,
'secret': secret
}
user = authenticate(**credentials)
if user is None:
raise exceptions.AuthenticationFailed(_('Invalid username/password.'))
if not user.is_active:
raise exceptions.AuthenticationFailed(_('User inactive or deleted.'))
return (user, None) # authentication successful
class My_View(viewsets.ModelViewSet):
authentication_classes = (CustomAuthentication,)
...
现在,我正在尝试像这样访问端点:localhost/api/endpoint/?public=TEST&secret=TEST 但每次我得到"No credentials provided."。我需要做什么才能在这里进行身份验证?提前致谢!
【问题讨论】:
标签: django django-rest-framework