[英]Obtaining DRF-jwt token
Currently I am generating tokens manually, but I want to to use jwt
tokens, I followed the official docs and other references but I am still unable to figure out the problem. 目前,我正在手动生成令牌,但是我想使用jwt
令牌,我遵循了官方文档和其他参考资料,但仍然无法找出问题所在。
serializers.py , in which after authenticating token is generated manually. serializers.py ,其中在验证令牌后手动生成。
class UserLoginSerializer(serializers.ModelSerializer):
token = serializers.CharField(allow_blank=True, read_only=True)
class Meta:
model = User
fields = [
'username',
'password',
'token',
]
extra_kwargs = {"password":
{"write_only": True}
}
def validate(self, data):
username = data.get('username', None)
password = data.get('password', None)
try:
usern = Account.objects.get(username=username)
except ObjectDoesNotExist:
raise serializers.ValidationError("User does not exists")
if usern.check_password(password):
data["token"] = "asdasdasdasd"
else:
raise serializers.ValidationError("password invalid")
return data
urls.py urls.py
from django.conf.urls import url
from .views import AuthRegister, AuthLogin
from rest_framework_jwt.views import obtain_jwt_token
urlpatterns = [
url(r'^register/$', AuthRegister.as_view()),
url(r'^login/$', AuthLogin.as_view()),
url(r'^api-token-auth/', obtain_jwt_token),
]
In settings I have included 'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
在设置中,我包含了'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
I have used url(r'^api-token-auth/', obtain_jwt_token)
, but I am unable to figure out how will I generate jwt
tokens. 我已经使用了url(r'^api-token-auth/', obtain_jwt_token)
,但是我无法弄清楚如何生成jwt
令牌。 Please anyone help me out! 请任何人帮我!
You need to make a HTTP POST call to the API on the endpoint /api-token-auth/
. 您需要对端点/api-token-auth/
上的API进行HTTP POST调用。 In this call, the body will contain two keys - username
and password
. 在此调用中,正文将包含两个键username
和password
。
Example using cURL : 使用cURL的示例 :
curl -X POST -d username=abcd -d password=1234 http://ip:port/api-token-auth/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.