DJango api to send refresh and access tokens
up vote
0
down vote
favorite
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.
django frameworks
add a comment |
up vote
0
down vote
favorite
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.
django frameworks
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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.
django frameworks
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.
django frameworks
django frameworks
edited Nov 9 at 11:58
a_k_v
654112
654112
asked Nov 9 at 10:52
Polaiah Bodeddula
11
11
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
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)
sorry i don't get the difference between refresh and access token. What do you mean by refresh token ?
– Shakil
Nov 9 at 12:33
hi Shakil, i've written the code for generating access token only. My question is hw can i send both refresh and access token. in My api response i should be able to generate access token or refresh token based on the grant type See the below: ... grant_type=request.data.get('grant_type') if(grant_type == 'password'): ... # code to send both refresh token & access token elif(grant_type =='refresh_token'): refresh_token = request.data.get('refresh_token'); ... code to send access token for the given refresh token I hope u understood. pls help to get thanks
– Polaiah Bodeddula
Nov 9 at 12:37
are they same? refresh and access tokens?
– Polaiah Bodeddula
Nov 9 at 12:45
sorry i misunderstood the question.
– Shakil
Nov 9 at 12:52
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
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)
sorry i don't get the difference between refresh and access token. What do you mean by refresh token ?
– Shakil
Nov 9 at 12:33
hi Shakil, i've written the code for generating access token only. My question is hw can i send both refresh and access token. in My api response i should be able to generate access token or refresh token based on the grant type See the below: ... grant_type=request.data.get('grant_type') if(grant_type == 'password'): ... # code to send both refresh token & access token elif(grant_type =='refresh_token'): refresh_token = request.data.get('refresh_token'); ... code to send access token for the given refresh token I hope u understood. pls help to get thanks
– Polaiah Bodeddula
Nov 9 at 12:37
are they same? refresh and access tokens?
– Polaiah Bodeddula
Nov 9 at 12:45
sorry i misunderstood the question.
– Shakil
Nov 9 at 12:52
add a comment |
up vote
0
down vote
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)
sorry i don't get the difference between refresh and access token. What do you mean by refresh token ?
– Shakil
Nov 9 at 12:33
hi Shakil, i've written the code for generating access token only. My question is hw can i send both refresh and access token. in My api response i should be able to generate access token or refresh token based on the grant type See the below: ... grant_type=request.data.get('grant_type') if(grant_type == 'password'): ... # code to send both refresh token & access token elif(grant_type =='refresh_token'): refresh_token = request.data.get('refresh_token'); ... code to send access token for the given refresh token I hope u understood. pls help to get thanks
– Polaiah Bodeddula
Nov 9 at 12:37
are they same? refresh and access tokens?
– Polaiah Bodeddula
Nov 9 at 12:45
sorry i misunderstood the question.
– Shakil
Nov 9 at 12:52
add a comment |
up vote
0
down vote
up vote
0
down vote
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)
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)
answered Nov 9 at 12:17
Shakil
4391313
4391313
sorry i don't get the difference between refresh and access token. What do you mean by refresh token ?
– Shakil
Nov 9 at 12:33
hi Shakil, i've written the code for generating access token only. My question is hw can i send both refresh and access token. in My api response i should be able to generate access token or refresh token based on the grant type See the below: ... grant_type=request.data.get('grant_type') if(grant_type == 'password'): ... # code to send both refresh token & access token elif(grant_type =='refresh_token'): refresh_token = request.data.get('refresh_token'); ... code to send access token for the given refresh token I hope u understood. pls help to get thanks
– Polaiah Bodeddula
Nov 9 at 12:37
are they same? refresh and access tokens?
– Polaiah Bodeddula
Nov 9 at 12:45
sorry i misunderstood the question.
– Shakil
Nov 9 at 12:52
add a comment |
sorry i don't get the difference between refresh and access token. What do you mean by refresh token ?
– Shakil
Nov 9 at 12:33
hi Shakil, i've written the code for generating access token only. My question is hw can i send both refresh and access token. in My api response i should be able to generate access token or refresh token based on the grant type See the below: ... grant_type=request.data.get('grant_type') if(grant_type == 'password'): ... # code to send both refresh token & access token elif(grant_type =='refresh_token'): refresh_token = request.data.get('refresh_token'); ... code to send access token for the given refresh token I hope u understood. pls help to get thanks
– Polaiah Bodeddula
Nov 9 at 12:37
are they same? refresh and access tokens?
– Polaiah Bodeddula
Nov 9 at 12:45
sorry i misunderstood the question.
– Shakil
Nov 9 at 12:52
sorry i don't get the difference between refresh and access token. What do you mean by refresh token ?
– Shakil
Nov 9 at 12:33
sorry i don't get the difference between refresh and access token. What do you mean by refresh token ?
– Shakil
Nov 9 at 12:33
hi Shakil, i've written the code for generating access token only. My question is hw can i send both refresh and access token. in My api response i should be able to generate access token or refresh token based on the grant type See the below: ... grant_type=request.data.get('grant_type') if(grant_type == 'password'): ... # code to send both refresh token & access token elif(grant_type =='refresh_token'): refresh_token = request.data.get('refresh_token'); ... code to send access token for the given refresh token I hope u understood. pls help to get thanks
– Polaiah Bodeddula
Nov 9 at 12:37
hi Shakil, i've written the code for generating access token only. My question is hw can i send both refresh and access token. in My api response i should be able to generate access token or refresh token based on the grant type See the below: ... grant_type=request.data.get('grant_type') if(grant_type == 'password'): ... # code to send both refresh token & access token elif(grant_type =='refresh_token'): refresh_token = request.data.get('refresh_token'); ... code to send access token for the given refresh token I hope u understood. pls help to get thanks
– Polaiah Bodeddula
Nov 9 at 12:37
are they same? refresh and access tokens?
– Polaiah Bodeddula
Nov 9 at 12:45
are they same? refresh and access tokens?
– Polaiah Bodeddula
Nov 9 at 12:45
sorry i misunderstood the question.
– Shakil
Nov 9 at 12:52
sorry i misunderstood the question.
– Shakil
Nov 9 at 12:52
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53224306%2fdjango-api-to-send-refresh-and-access-tokens%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown