You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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';constxero=newXeroClient({clientId: 'YOUR_CLIENT_ID',clientSecret: 'YOUR_CLIENT_SECRET',redirectUris: [`http://localhost/:${port}/callback`],scopes: 'openid profile email accounting.transactions offline_access'.split(" ")});awaitxero.initialize();consttokenSet=getTokenSetFromDatabase(userId);// example function nameawaitxero.setTokenSet(tokenSet);// Set the token inside the xero clientif(tokenSet.expired()){constvalidTokenSet=awaitxero.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_getterdefobtain_xero_oauth2_token():
returnsession.get("token")
@api_client.oauth2_token_saverdefstore_xero_oauth2_token(token):
session["token"] =tokensession.modified=True# get existing token settoken_set=get_token_set_from_database(user_id); //examplefunctionname# set token set to the api clientstore_xero_oauth2_token(token_set)
# refresh token set on the api clientapi_client.refresh_oauth2_token()
Hi @barrachri I'm not familiar with celery but if you can share a hello world project that I can run locally, I'll give it a go. We use the session to store the token in our sample apps for the sake of simplicity but it's not required. You just need to configure the exchange point using the decorators to get and set the token on the api client, but you could be getting/saving the token from/to a db instead
Hey @barrachri an update on this. Your understanding was correct - currently you'd have to set the token as a global.
Currently the team is focused on squashing bugs and keeping parity with the public API. Since this is more of a feature/enhancement request, it's going to be on the back burner, however, if you want to submit a PR implementing the feature, we'd be happy to review and merge and arrange some Xero swag for your contribution 🙂
Activity
RettBehrens commentedon Aug 16, 2022
Hi @barrachri I'm not familiar with celery but if you can share a hello world project that I can run locally, I'll give it a go. We use the session to store the token in our sample apps for the sake of simplicity but it's not required. You just need to configure the exchange point using the decorators to get and set the token on the api client, but you could be getting/saving the token from/to a db instead
barrachri commentedon Aug 17, 2022
Hi @RettBehrens, thanks for your answer.
how would you implement something like this?
From what I understood the only way is this one
If that's the only way, I think it would be nice to add a
set_tokenlike the js.RettBehrens commentedon Sep 28, 2022
Hey @barrachri an update on this. Your understanding was correct - currently you'd have to set the token as a global.
Currently the team is focused on squashing bugs and keeping parity with the public API. Since this is more of a feature/enhancement request, it's going to be on the back burner, however, if you want to submit a PR implementing the feature, we'd be happy to review and merge and arrange some Xero swag for your contribution 🙂
joe-niland commentedon Jan 20, 2023
You've probably solved this by now but the ApiClient constructor allows you to pass in functions for token get/set like this:
In these you can store/retrieve the token any way you like.
barrachri commentedon Jan 20, 2023
Thanks @joe-niland!
jaredthecoder commentedon Apr 26, 2023
@joe-niland this is what we have done to handle this the past couple years. Confirming for others. :)
joe-niland commentedon Apr 26, 2023
@rdemarco-xero I think this issue can be closed