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 for machine-to-machine implementation #315

Open
gregoriopellegrino opened this issue Jul 19, 2021 · 2 comments
Open

Example for machine-to-machine implementation #315

gregoriopellegrino opened this issue Jul 19, 2021 · 2 comments

Comments

@gregoriopellegrino
Copy link

Hi,
I'm trying to convert a Private Application to a Machine-to-Machine implementation, but I can't find how to implement it. Is there documentation or examples?

Thanks,
Gregorio

@claudechenth
Copy link

claudechenth commented Aug 23, 2021

Hi @gregoriopellegrino , I am in the same situation. While waiting for them to implement the newly released custom connection authentication, this is my temporary solution. The idea basically is to pass an initial access token into the oauth2 credentials. Hope this helps.

Here's the example

from xero import Xero
from xero.auth import OAuth2Credentials
import requests

class CustomConnetionsOAuth2Credentails(OAuth2Credentials):

    def __init__(self, client_id, client_secret, tenant_id=None):
        token = requests.post('https://identity.xero.com/connect/token', 
            data={
                "grant_type": "client_credentials",
            },
            auth=(client_id, client_secret)).json()
        if tenant_id is None:
            # Get Default TenantID
            tenant_id = requests.get("https://api.xero.com/connections", 
                headers = {"Authorization": f"Bearer {token['access_token']}"}).json()[0]['tenantId']
        super().__init__(
            client_id=client_id, 
            client_secret=client_secret,
            token=token,
            tenant_id=tenant_id)


class MyXeroClient:

    def __init__(self):
        credentials = CustomConnetionsOAuth2Credentails(
            client_id=<client_id>, 
            client_secret=<client_secret>
        )
        self.xero = Xero(credentials)

    ....

@talhajunaidd
Copy link

we don't really need tenant id. we can get away with a dummy string.

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

3 participants