Using the refresh token

Here we’ll see, step by step, how to use saved tokens from a previous login.

In the following steps, I will admit you saved somewhere your tokens, closed the previous python interpreter and opened a new one.

Before starting this example, be sure you saved the tokens from the SSO Login Example - Step 4 as we will use them here.

 

Step 1 - Creating EsiPy objects

Like in the login example, we’ll need to create the esipy objects before beeing able to do anything.

from esipy import EsiApp
from esipy import EsiClient
from esipy import EsiSecurity

app = EsiApp().get_latest_swagger

# replace the redirect_uri, client_id and secret_key values
# with the values you get from the STEP 1 !
security = EsiSecurity(
    redirect_uri='callback URL',
    client_id='you client id',
    secret_key='the_secret_key',
)

# and the client object, replace the header user agent value with something reliable !
client = EsiClient(
    retry_requests=True,
    header={'User-Agent': 'Something CCP can use to contact you and that define your app'},
    security=security
)
If you don't know where to get the CALLBACK URL, CLIENT ID and SECRET KEY, please take a look at the SSO Login Example - Step 2.

 

Step 2 - Updating the security object

Now everything is ready, it’s time to update the security object.

As it probably pasts more than 20minute, we’ll assume the access_token is expired so we will only care about the refresh_token

# to update the security object, 
security.update_token({
    'access_token': '',  # leave this empty
    'expires_in': -1,  # seconds until expiry, so we force refresh anyway
    'refresh_token': 'YOUR_SAVED_REFRESH_TOKEN'
})

Now that we updated the security object, we have 2 choices:

  • Either refresh the access token manually, so we can store the new token (better)
  • Leave it like that and it will update itself with the first EsiClient.request()

 

Step 3 - Updating the tokens

So we want to manually refresh the tokens, we just have to call one method to do this:

tokens = security.refresh()

The tokens variable now contains your access token, refresh token, and the seconds left until expiry.
By doing this, your security object also knows these tokens and will use them automatically when doing requests

print tokens

{
  "access_token": "frenafeifafrbaefluerbfeainb",
  "token_type": "Bearer",
  "expires_in": 1200,
  "refresh_token": "fera48ftea4at64fr684fae"
}
Now you can make a query like in the SSO Login Example - Step 5.
You can now see another example using Flask + Flask Login and sqlite as a database.