Keycloak refresh token returns "Stale token"

58 views Asked by At

So I have my application, in the FE, that calls keycloak BE in the login:

    @app.post("/login")
    async def login(form_user:str,form_pass:str, response: Response):
    
        token_response= keycloak_openid.token(grant_type="password", username=form_user, password=form_pass)
    
        generated_token.access_token=token_response["access_token"]
        generated_token.refresh_token= token_response["refresh_token"]
        generated_token.expiration_acess_token = token_response["expires_in"]
    
        response = JSONResponse(content = credentials)
    
        response.set_cookie(key="refresh_token", value=generated_token.refresh_token, httponly=True,samesite='none')
    
        return credentials
    enter code here

and then since I saved the cookie in the FE, I receive it in the back_end:

 def verify_login(req: Request):
        token= req.headers["Authorization"]
        #logging.info(f'This is the accession token: {token}')
        refresh_token= req.cookies.get('refresh_token')
        print("refresh_token", refresh_token)

But now I want to from this refresh_token generate another access_token:

 payload_refresh_token=f'client_id={settings.client_id}&client_secret={settings.client_secret}&refresh_token={refresh_token_header}&grant_type=refresh_token'
        headers = {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
        reffresh_token_url = requests.post(session_user_refresh_url, headers=headers, data=payload_refresh_token)

but when I do this I have this error:new token:

{'error': 'invalid_grant', 'error_description': 'Stale token'}
0

There are 0 answers