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.










share|improve this question




























    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.










    share|improve this question


























      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.










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 9 at 11:58









      a_k_v

      654112




      654112










      asked Nov 9 at 10:52









      Polaiah Bodeddula

      11




      11
























          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)





          share|improve this answer





















          • 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











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          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

























          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)





          share|improve this answer





















          • 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















          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)





          share|improve this answer





















          • 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













          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)





          share|improve this answer












          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)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          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


















          • 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


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Schultheiß

          Verwaltungsgliederung Dänemarks

          Liste der Kulturdenkmale in Wilsdruff