0

I would like to use xero-python My app is connected on Xero developper. So I was able to connect and save token and Ids for scope: scope = 'offline_access accounting.journals.read'

I use this to Xero requests :

def XeroRequests():
    old_refresh_token = open(dst_dir,'r').read()
    new_token = XeroRefreshToken(old_refresh_token)
    xero_tenant_id = XeroTenants(new_token[0])
    rt_file = open(tenant_dir, 'w')
    rt_file.write(xero_tenant_id)
    rt_file.close()

with refresh token:

def XeroRefreshToken(refresh_token):
    token_refresh_url = 'https://identity.xero.com/connect/token'
    response = requests.post(token_refresh_url,
                             headers={
                                 'Authorization': 'Basic ' + b64_id_secret,
                                 'Content-Type': 'application/x-www-form-urlencoded'
                             },
                             data={
                                 'grant_type': 'refresh_token',
                                 'refresh_token': refresh_token
                             })
    json_response = response.json()
    #print(json_response)

    new_refresh_token = json_response['refresh_token']
    rt_file = open(dst_dir, 'w')
    rt_file.write(new_refresh_token)
    rt_file.close()

    new_access_token = json_response['access_token']
    rt_file = open(acces_token_dir, 'w')
    rt_file.write(new_access_token)
    rt_file.close()

Now I want only to get data from Xero like this:

import dateutil
import os
from xero_python.accounting import AccountingApi
from xero_python.api_client import ApiClient, serialize
from xero_python.api_client.configuration import Configuration
from xero_python.api_client.oauth2 import OAuth2Token
from xero_python.exceptions import AccountingBadRequestException
from utils.prelimnaryXeroReports import XeroRequests

basedir = os.path.abspath(os.path.dirname(__file__))
dst_dir=basedir + f'/utils/data/refresh_token.txt'
tenant_dir=basedir + f'/utils/data/tenantId.txt'
acces_token_dir=basedir + f'/utils/data/accesstokenId.txt'

App_Name = 'DataX'
client_id = 'xxxxxx'
client_secret = 'xxxxxxx'
scope = 'offline_access accounting.journals.read'

test = XeroRequests()
refresh_token = open(dst_dir,'r').read()
xero_tenant_id = open(tenant_dir,'r').read()
xero_access_token_id = open(acces_token_dir,'r').read()

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


def accounting_get_accounts(xero_tenant_id):
    api_instance = AccountingApi(api_client)
    xero_tenant_id = xero_tenant_id
    if_modified_since = dateutil.parser.parse("2020-02-06T12:17:43.202-08:00")
    where = 'Status=="ACTIVE" AND Type=="BANK"'
    order = 'Name ASC'

    try:
        api_response = api_instance.get_accounts(xero_tenant_id, if_modified_since, where, order)
        print(api_response)
    except AccountingBadRequestException as e:
        print("Exception when calling AccountingApi->getAccounts: %s\n" % e)

test = accounting_get_accounts(xero_tenant_id)

But when I launch this code, I've this error: xero_python.exceptions.OAuth2TokenGetterError: Invalid oauth2_token_getter=None function

How can I fix this or is this another way to get data like journals, invoices, chart of accounts, without using flask (as I will automate this get app every night. Thanks

1 Answers1

0

You need decorator function "oauth2_token_getter".

Reference: https://github.com/XeroAPI/xero-python

Make return value of the function using your own to obtain access token, maybe from config file, json, conf or something else.

@api_client.oauth2_token_getter
def obtain_xero_oauth2_token():
    return yourOwnFunctiontoGetAccessToken()