XeroAPI / xero-python-custom-connections-starter

Custom Connections starter app for xero-python and client_credentials grant
MIT License
1 stars 6 forks source link

Example without flask #1

Open ari opened 2 years ago

ari commented 2 years ago

I don't want to use flask in my integration scripts, which is why I'm paying an additional $10 for the custom connection.

Please supply an example of how to get the token without flask.

    api_client = ApiClient(
        Configuration(
            debug=False,
            oauth2_token=OAuth2Token(
                client_id=CLIENT_ID, client_secret=CLIENT_SECRET
            ),
        ),
        pool_threads=1,
    )

throws an error:

xero_python.exceptions.OAuth2TokenSaverError: Invalid oauth2_token_saver=None function

ari commented 2 years ago

This seems to do the trick

class XeroIntegration:
    token = None

    def __init__(self):
        api_client = ApiClient(
            Configuration(
                debug=False,
                oauth2_token=OAuth2Token(
                    client_id=CLIENT_ID, client_secret=CLIENT_SECRET
                ),
            ),
            pool_threads=1,
            oauth2_token_getter=self.obtain_xero_oauth2_token,
            oauth2_token_saver=self.store_xero_oauth2_token,
        )
        xero_token = api_client.get_client_credentials_token()
        if xero_token is None or xero_token.get("access_token") is None:
            raise "Access denied: response=%s" % xero_token

    def obtain_xero_oauth2_token(self):
        return self.token

    def store_xero_oauth2_token(self, token):
        self.token = token

if __name__ == '__main__':
    x = XeroIntegration()
    print(x.token)
bishwadeep commented 2 years ago

Hey @ari I raised an issue but could not get any reply. Hopefully you will be able to help me. Thank you so much for the code above. Finally after 2 months I was able to get token. Nothing helped except your code above and xero support team and API team did not care to help me. Thank you once again. I ran your code individually and I was able to get the access token. I run this using cron job, it was absolutely working fine in oauth 1.0 but I could not make it work for oauth 2.0. How do I integrate your code in my case? I dont want to use flask because I am using the cron job to run it and I got lot of issues using flask which I was not able to resolve.

I have the following code: `def invoiceGetActiveProviders(): xero_tenant_id = "Replaced by my tenant id" xero_client = ApiClient( Configuration( debug=False, oauth2_token=OAuth2Token( client_id="REPLACED BY CLIENT ID WORKING FINE", client_secret="REPLACED BY SECRET WORKING FINE" ), ), pool_threads=1, ) accounting_api = AccountingApi(xero_client)

my oauth v1 code to create invoice

accounting_api.create_invoices(xero_tenant_id, invoice, True)

`

bishwadeep commented 2 years ago

Now receiving update_token() argument after ** must be a mapping, not str