我正在尝试在Django Rest Framework上设置自定义身份验证类,但所有客户端请求都返回相同的错误:
{“detail”:“未提供身份验证凭据。”}
因此,调试Custom类并打印请求.META atributte它没有 X_USERNAME键。
CustomAuth类:
$formatter = new NumberFormatter('fr_FR', NumberFormatter::CURRENCY);
$p = $formatter->getPattern(); // "#,##0.00 ¤;(#,##0.00 ¤)"
$p = explode(";", $p, 2);
$formatter->setPattern($p[0]); // "#,##0.00 ¤"
echo $formatter->formatCurrency('-1.23', 'EUR');
在settings.py文件上:
class TecnicoAuthentication(authentication.BaseAuthentication):
def authenticate(self, request):
username = request.META.get('X_USERNAME')
if not username:
return None
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
raise exceptions.AuthenticationFailed('No such user')
return (user,None)
}
在apache配置文件中:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES' : (
'os_hotlink.auth.TecnicoAuthentication',
),
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
'DEFAULT_RENDERER_CLASSES': (
'rest_framework.renderers.JSONRenderer',
)
最后使用httpie:
的请求方法 WSGIPassAuthorization On
答案 0 :(得分:1)
正在寻找名为“X_USERNAME”的自定义请求标头。
因此,您需要在HTTpie请求中名为X_USERNAME的自定义请求标头中定义用户...
我认为如果你有用户名'user',那么你应该尝试发送它:
http 127.0.0.1 X_USERNAME:user
HTTP标头
要设置自定义标题,您可以使用标题:值表示法:
$ http example.org User-Agent:PoopyPants 'Cookie:valued-visitor=yes;whatever=whatever;etc=etc' \
X_USERNAME:user Referer:http://stackoverflow.com/
GET / HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Cookie: valued-visitor=yes;whatever=whatever;etc=etc
Host: 127.0.0.1
Referer: http://stackoverflow.com/
User-Agent: PoopyPants
X_USERNAME: user