简体   繁体   English

DJango API发送刷新和访问令牌

[英]DJango api to send refresh and access tokens

def login(request):

    username = request.data.get("username")
    password = request.data.get("password")
    if username is None or password is None:
        return Response({'error': 'Please provide both username and password'},
                        status=HTTP_400_BAD_REQUEST)
    user = authenticate(username=username, password=password)
    if not user:
        return Response({'error': 'Invalid Credentials'},
                        status=HTTP_404_NOT_FOUND)
    token, _ = Token.objects.get_or_create(user=user)
    return Response({'token': token.key},
                    status=HTTP_200_OK)

Hi, How can i modify this code to send refresh token and access token for first time and send access token by receiving refresh token. 嗨,我如何修改此代码以第一次发送刷新令牌和访问令牌,并通过接收刷新令牌发送访问令牌。 please help to do that. 请帮助做到这一点。 thanks in advance. 提前致谢。

I don't think this is possible without destroying your previous generated token. 我认为如果不破坏先前生成的令牌,这是不可能的。 If you want new authtoken every time you login here you can ensure first that you destroy the earlier token and create a new one. 如果您每次在此处登录都需要新的身份验证令牌,则可以确保首先销毁较早的令牌并创建一个新的令牌。

 from django.core.exceptions import ObjectDoesNotExist
 try:
     token = Token.objects.get(user=user)
     token.delete()
     token = Token.objects.create(user=user)

 except ObjectDoesNotExist:
     token = Token.objects.create(user=user)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Django 中安全地存储 OAuth2 访问和刷新令牌 - How to securely store OAuth2 access and refresh tokens in Django Django:身份验证凭据未提供错误,因为访问和刷新令牌设置为 HttpOnly cookies 没有授权 header - Django: Authentication credentials not povided error as access and refresh tokens are set as HttpOnly cookies without Authorization header Django REST:如何使用自定义声明将 SimpleJWT 访问和刷新令牌作为 HttpOnly cookies 返回? - Django REST: How do i return SimpleJWT access and refresh tokens as HttpOnly cookies with custom claims? Django Rest API Djoser 刷新和访问令牌问题 - Django Rest API Djoser Refresh and Access Token Problem AJAX 调用不适用于向 Django 中的 API 发送令牌 - AJAX call not working for sending tokens to API in Django 如何在 user.save() 之后验证并返回访问和刷新令牌 - How to validate and return access and refresh tokens after user.save() 单令牌刷新与长时间运行的刷新令牌(Django GraphQL JWT) - Single token refresh vs Long running refresh tokens (Django GraphQL JWT) 如何在 Django 的单个 API 中获取访问令牌和刷新令牌(rest_framework_jwt) - How to get access token and refresh token ( rest_framework_jwt ) in a single API in Django Django创建非身份验证一次性使用访问令牌 - Django creating non-auth single use access tokens 无法访问 django API - Unable to access django API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM