title: How to properly build json web token in python
body: <p>Am trying to build Json web token in python django using
  <a href="https://github.com/jpadilla/pyjwt" rel="nofollow noreferrer">pyjwt library</a> by referencing suggestion by <strong>Madhu_Kiran_K</strong> from stackoverflow
  <a href="https://stackoverflow.com/questions/38635092/how-to-create-json-web-token-to-user-login-in-django-rest-framework">link</a></p>
  
  <p>In the code below, Am trying Generates a JSON Web Token that stores this user's ID, email and has an expiry date set to 2 days into the future.
  I can succesfully encode and decode the token to get all the required users information and token expiration time.</p>
  
  <p><strong>My Requirement Now:</strong></p>
  
  <p>1.)Please how do  I validate and ensure that the token sent by client is still valid and hence not tampered.</p>
  
  <p>2.)how to check for expiration time and print a message.
  Here is what I have done to that effect</p>
  
  <pre><code>#check if the token were tampered
  
  if content['user_id'] =='' or content['email'] == '':
      print("Invalid Token")
  else:
      print("Token is still valid")
  
  #check if token has expired after 2 days
  
      if content['exp'] &gt; dt:
          print("Token is still active")
      else:
          print("Token expired. Get new one")
  </code></pre>
  
  <p>The time expiration checking returns <strong>error message not supported between instances of int and datetime.datetime.</strong> What is the proper way to validate. Thanks</p>
  
  <p><strong>below is the full code</strong></p>
  
  <pre><code>#!/usr/bin/python
  
  import sys
  import re
  import json
  
  from datetime import datetime, timedelta
  import jwt
  
  
  #https://www.guru99.com/date-time-and-datetime-classes-in-python.html
  #timedelta(days=365, hours=8, minutes=15)
  #'exp': int(dt.strftime('%s'))
  
  
  dt = datetime.now() + timedelta(days=2)           
  encoded_token = jwt.encode({'user_id': "abc", 'email': "nancy@gmail.com", 'exp': dt }, 'MySECRET goes here', algorithm='HS256')
  print(encoded_token)
  
  #decode above token
  decode_token=jwt.decode(encoded_token, 'MySECRET goes here', algorithms=['HS256'])
  
  content = decode_token
  print(content)
  print(content['user_id'])
  print('json token successfully retrieved')
  
  if content['user_id'] =='' or content['email'] == '':
      print("Invalid Token")
  else:
      print("Token is still valid")
  
  #check if token has expired after 2 days
  
      if content['exp'] &gt; dt:
          print("Token is still active")
      else:
          print("Token expired. Get new one")
  </code></pre>