“未提供身份验证详细信息。” Django jwt

huangapple go评论76阅读模式
英文:

"Authentication details were not provided." Django jwt

问题

我为获取用户配置文件的详细信息创建了一个视图

user_views.py
@api_view(['GET'])
@permission_classes([IsAuthenticated])
def getUserProfile(request):
    user = request.user
    serializer = UserSerializer(user, many=False)
    return Response(serializer.data)

urls.py

urlpatterns = [
    path("/users/profile/", uv.getUserProfile, name="get-user-profile")
]

serializers.py

class UserSerializer(serializers.ModelSerializer):
    name = serializers.SerializerMethodField(read_only=True)
    isAdmin = serializers.SerializerMethodField(read_only=True)
  
    class Meta:
        model = User
        fields = ['id', 'username', 'email', 'name', 'isAdmin']
  
    def get_id(self, obj):
        return obj.id
  
    def get_isAdmin(self, obj):
        return obj.is_staff
  
    def get_name(self, obj):
        name = obj.first_name
        if name == '':
            name = obj.email
  
        return name

class UserSerializerWithToken(UserSerializer):
    token = serializers.SerializerMethodField(read_only=True)
  
    class Meta:
        model = User
        fields = ['id', 'username', 'email', 'name', 'isAdmin', 'token']
  
    def get_token(self, obj):
        token = RefreshToken.for_user(obj)
        return str(token.access_token)

通过Postman发送了带有 Bearer <token> 授权标头的 GET 请求,并获得了以下响应:

{
    "detail": "未提供身份验证详细信息。"
}

使用了 django-rest-frameworkdjango-rest-framework-simplejwt


<details>
<summary>英文:</summary>

I&#39;ve created a view for getting details of the user&#39;s profile. 

user_views.py
```py
@api_view([&#39;GET&#39;]) 
@permission_classes([IsAuthenticated]) 
def&#160;getUserProfile(request): 
 &#160;&#160;&#160;user&#160;=&#160;request.user 
 &#160;&#160;&#160;&#160;serializer&#160;=&#160;UserSerializer(user,&#160;many=False) 
 &#160;&#160;&#160;return&#160;Response(serializer.data)

urls.py

urlpatterns = [
    path(&quot;/users/profile/&quot;, uv.getUserProfile, name=&quot;get-user-profile&quot;)
]

serializers.py

class&#160;UserSerializer(serializers.ModelSerializer): 
 &#160;&#160;&#160;&#160;name&#160;=&#160;serializers.SerializerMethodField(read_only=True) 
 &#160;&#160;&#160;&#160;isAdmin&#160;=&#160;serializers.SerializerMethodField(read_only=True) 
  
 &#160;&#160;&#160;&#160;class&#160;Meta: 
 &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;model&#160;=&#160;User 
 &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;fields&#160;=&#160;[&#39;id&#39;,&#160;&#39;username&#39;,&#160;&#39;email&#39;,&#160;&#39;name&#39;,&#160;&#39;isAdmin&#39;] 
  
 &#160;&#160;&#160;&#160;def&#160;get_id(self,&#160;obj): 
 &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;return&#160;obj.id 
  
 &#160;&#160;&#160;&#160;def&#160;get_isAdmin(self,&#160;obj): 
 &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;return&#160;obj.is_staff 
  
 &#160;&#160;&#160;&#160;def&#160;get_name(self,&#160;obj): 
 &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;name&#160;=&#160;obj.first_name 
 &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;if&#160;name&#160;==&#160;&#39;&#39;: 
 &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;name&#160;=&#160;obj.email 
  
 &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;return&#160;name 
  
  
 class&#160;UserSerializerWithToken(UserSerializer): 
 &#160;&#160;&#160;&#160;token&#160;=&#160;serializers.SerializerMethodField(read_only=True) 
  
 &#160;&#160;&#160;&#160;class&#160;Meta: 
 &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;model&#160;=&#160;User 
 &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;fields&#160;=&#160;[&#39;id&#39;,&#160;&#39;username&#39;,&#160;&#39;email&#39;,&#160;&#39;name&#39;,&#160;&#39;isAdmin&#39;,&#160;&#39;token&#39;] 
  
 &#160;&#160;&#160;&#160;def&#160;get_token(self,&#160;obj): 
 &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;token&#160;=&#160;RefreshToken.for_user(obj) 
 &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;return&#160;str(token.access_token)

I'm sending a get request through post man and sending a authorization header with Bearer &lt;token&gt;. And got

{
    &quot;detail&quot;: &quot;Authentication details were not provided.&quot;
}

Using django-rest-framework and django-rest-framework-simplejwt.

答案1

得分: 0

Figured it out like after a bit. Needed to add

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework_simplejwt.authentication.JWTAuthentication',
    ],
}

to settings.py

英文:

Figured it out like after a bit. Needed to add

REST_FRAMEWORK = {
    &#39;DEFAULT_AUTHENTICATION_CLASSES&#39;: [
        &#39;rest_framework_simplejwt.authentication.JWTAuthentication&#39;,
    ],
}

to settings.py

huangapple
  • 本文由 发表于 2023年2月18日 18:32:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75492724.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定