Open
Description
Hi there, thanks for making it easier to connect with xero!
I am trying to understand how to leverage xero-python to access multiple organizations.
Let's consider that I have 2 users, who are trying to access different organizations/tenants.
Now all the examples I saw cover using a flask session to store the token. But that solution won't work in a celery task or background job.
I was checking the node client, and it makes super easy to set the token:
import { XeroClient } from 'xero-node';
const xero = new XeroClient({
clientId: 'YOUR_CLIENT_ID',
clientSecret: 'YOUR_CLIENT_SECRET',
redirectUris: [`http://localhost/:${port}/callback`],
scopes: 'openid profile email accounting.transactions offline_access'.split(" ")
});
await xero.initialize();
const tokenSet = getTokenSetFromDatabase(userId); // example function name
await xero.setTokenSet(tokenSet); // Set the token inside the xero client
if(tokenSet.expired()){
const validTokenSet = await xero.refreshToken();
// save the new tokenset
}
while with the python client you are forced to use obtain_xero_oauth2_token
and store_xero_oauth2_token
as a sort of global cache:
# configure token persistence and exchange point between app session and xero-python
@api_client.oauth2_token_getter
def obtain_xero_oauth2_token():
return session.get("token")
@api_client.oauth2_token_saver
def store_xero_oauth2_token(token):
session["token"] = token
session.modified = True
# get existing token set
token_set = get_token_set_from_database(user_id); // example function name
# set token set to the api client
store_xero_oauth2_token(token_set)
# refresh token set on the api client
api_client.refresh_oauth2_token()
any idea on how to avoid this?