To assist with the development of your Lucid integration, we have provided an example python script that illustrates the typical refresh token flow. It stores the access_token
and refresh_token
into a local file, and automatically refreshes them upon expiration. It is comprised of four steps:
- Check if the file containing a previous refresh token already exists. If it does not, we need to initiate the process by using the value provided in
INITIAL_REFRESH_TOKEN
to generate a token and store it locally. TheINITIAL_REFRESH_TOKEN
can be manually generated by following the steps at: Refreshing the access token. - Retrieve the current access_token and refresh_token from the file. If the current access_token has not expired, return it and skip the remaining steps. Otherwise, move onto step 3 to refresh the access_token.
- Using the retrieved refresh_token, make a request to the
/token
endpoint with the refresh_tokengrant_type
. Upon receiving the response, it calls step 4 to save the both tokens for future use and returns the now refreshed access_token. - Before storing the data, utilize the provided
expires_in
value to generate an ISO format datetime so we know the absolute time our access_token will expire. Store the access_token, refresh_token, and expiration into our file.
Important
For simplicity, this script does not include response code verification or error checking. A production version of this script should handle unexpected values.
#!python
import json
import requests
import os
from datetime import datetime, timedelta
# using local file to store refresh_token
FILE_NAME = './token_info.json'
# oauth2 client information
CLIENT_ID = '30VYbvlkqZv-SmJd7fTdpH9B9et2yqZA6Wvi5NY_'
CLIENT_SECRET = 'D-SkY5dE9m7hQApMwc9DV0iCzSRVV62MnRvG6KKOZt4vNpcw8mTVxM8T9x7qpV72xLEiw'
# initial refresh token generated manually
INITIAL_REFRESH_TOKEN = 'oauth2-fzLTc4MjRiNGY5ZGI4MjlmZWNlZjI5OG...'
# 1) Return our current token, or generate a new one if its expired.
# If this is the first execution, use a manually generated token to get started
def retrieve_access_token():
if (os.path.exists(FILE_NAME)):
return retrieve_token_info(None)
else:
return retrieve_token_info(INITIAL_REFRESH_TOKEN)
# 2) Retrieve the current token and refresh if its about to expire
def retrieve_token_info(grantedToken):
if (grantedToken is not None):
return refresh_token(grantedToken)
else:
with open(FILE_NAME, 'r') as fileHandle:
data = json.loads(fileHandle.read())
if datetime.fromisoformat(data['expiration']) > datetime.now()- timedelta(minutes=5):
return data['access_token']
return refresh_token(data['refresh_token'])
# 3) Refresh my token and save for future use
def refresh_token(refresh_token):
url = 'https://api.lucid.co/oauth2/token'
body = {
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
'grant_type':'refresh_token',
'refresh_token': refresh_token
}
request = requests.post(url, json = body)
response = json.loads(request.content)
save_token_info(response)
return response['access_token']
# 4) Set the expiration and save the token for next time
def save_token_info(token):
expiration = datetime.now() + timedelta(seconds=token.get('expires_in'))
token['expiration'] = expiration.isoformat()
with open(FILE_NAME, 'w') as fileHandle:
fileHandle.write(json.dumps(token))
# get my current or refreshed access_token
access_token = retrieve_access_token()
print(access_token)