2017-09-23 113 views
0

目前我正在手動生成令牌,但我想使用jwt令牌,我遵循官方文檔和其他參考,但我仍然無法找出問題所在。獲取DRF-jwt令牌

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

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), 
] 

在設置我已經包括'rest_framework_jwt.authentication.JSONWebTokenAuthentication',

我用url(r'^api-token-auth/', obtain_jwt_token),但我無法弄清楚我將如何產生jwt令牌。 請任何人幫助我!

回答

0

您需要對端點/api-token-auth/上的API進行HTTP POST調用。在這次電話會議上,機構將包含兩個鍵 - usernamepassword

使用捲曲實施例:

curl -X POST -d username=abcd -d password=1234 http://ip:port/api-token-auth/