Creating Token with OAuth2
pip install "python-jose[cryptography]"from jose import JWTError, jwtfrom jose import JWTError, jwt
SECRET_KEY = "09d25e094faa6ca2556c818166b7a9563b93f7099f6f0f4caa6cf63b88e8d3e7"
ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES = 30from jose import JWTError, jwt
from datetime import date, datetime, timedelta
SECRET_KEY = "09d25e094faa6ca2556c818166b7a9563b93f7099f6f0f4caa6cf63b88e8d3e7"
ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES = 30
def create_access_token(data: dict):
# We do not want to modify the actual data, so we create a copy
to_encode = data.copy()
# We use the datetime.now() so we can update the time to expiry
expire = datetime.now() + timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
# to_encode is a dirctionary so we are updating this with the expiration time
to_encode.update({"exp": expire})
# Now as we have the data, we can return the JWT by using the .encode method
# and providing the data, the secret key, and the algorithm in the method
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
return encoded_jwt
Last updated