简体   繁体   English

获取DRF-jwt令牌

[英]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 . 在此调用中,正文将包含两个键usernamepassword

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.

相关问题 使用DRF-JWT检索Postman中的列表测试,确定DRF中的IsAuthenticated - Determining IsAuthenticated in DRF Using DRF-JWT to retrieve a list testing with Postman DRF 测试中 jwt 令牌的身份验证错误 401 - Authentication error 401 for jwt token in DRF tests 如何在 drf 中通过访问 jwt 令牌向用户发送它 - how to send user it with access jwt token in drf 何时在 DRF 中的 jwt 中调用 gettoken、刷新令牌和验证令牌? - When to call gettoken, refresh token and verify token by user in jwt in DRF? DRF JWT 在 OPTIONS 请求上不需要令牌 - DRF JWT Don't require token on OPTIONS-requests 在没有 DRF 的表单提交上的 Django 视图中生成 JWT 令牌 - Generate JWT token in Django view on form submit without DRF DRF 简单 jwt。 如何更改来自 TokenObtainPairView 的响应以获取访问令牌过期时间 - DRF simple jwt. How to change response from TokenObtainPairView to get an access token EXPIRES time DRF如何关闭基于JWT的身份验证的CSRF令牌检查? - How does DRF turn off CSRF-token check for JWT-based authentication? DRF 简单 jwt 从用户实例获取身份验证令牌或更改用户名和密码组合 - DRF simple jwt getting auth token from a user instance OR change username and password combination Django DRF JWT身份验证获取令牌-JSON解析错误-期望值 - Django DRF JWT authentication get token - JSON parse error - Expecting value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM