Search code examples
pythonauthenticationjwtauth0

API token session expired every 3 days


I am new to programming but I would like to ask about API. I am using a third party API token and bearer: auth, where it will expire every 3 days then I have to generate a new API token. I was wondering if you have an idea on how to make it permanent so I don't have to keep going to the third party API generator?

Headers = {
     'X-CSRFToken': 'token',
     'Authorization': 'Bearer authorization'
}

url = 'url'

response= requests.get(url, headers=Headers, timeout=20)

How can I make it permanent so it doesn't expire? On the third party API I am using specific part of the auto generate API to generate specific data.


Solution

  • It sounds like you need OAuth for this. There are two ways to approach this and it depends on whether or not you have a user present or not using your application that needs to call this third party API. Either that API should be giving you a client ID and secret that you can use for Client Credentials Grant (this is if you don't have a user present, this tends to be for backend services that are running jobs while no one is around). If you have users signing into your application. You would want to Authorization Code Flow (with offline_access scope) to get an access token and a refresh token so that you can get new access tokens on their behalf when they aren't around anymore. You would want to store that information in your own system (carefully as these are powerful tokens) and then you can get fresh access tokens automatically.

    The API you are attempting to call on behalf of this user should have instructions for how to retrieve new tokens on the user's behalf without having to go to an Access Token generation tool. If you are the user, you can follow the instructions yourself programmatically in your application.