Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example without flask #1

Open
ari opened this issue Nov 23, 2021 · 3 comments
Open

Example without flask #1

ari opened this issue Nov 23, 2021 · 3 comments

Comments

@ari
Copy link

ari commented Nov 23, 2021

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
Copy link
Author

ari commented Nov 23, 2021

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
Copy link

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants