import requests import base64 import json client_id = 'fakeclient_id' client_secret = 'fakeclient_secret' redirect_url = 'callbackurl here' b64_id_secret = base64.b64encode(bytes(client_id + ':' + client_secret, 'utf-8')).decode('utf-8') def XeroTenants(access_token): connections_url = 'https://api.xero.com/connections' response = requests.get(connections_url, headers = { 'Authorization': 'Bearer ' + access_token, 'Content-Type': 'application/json' }) json_response = response.json() for tenants in json_response: json_dict = tenants return json_dict['tenantId'] 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() new_refresh_token = json_response['refresh_token'] rt_file = open('refresh_token.txt', 'w') rt_file.write(new_refresh_token) rt_file.close() return [json_response['access_token'], json_response['refresh_token']] def find(InvoiceNumber): old_refresh_token = open('refresh_token.txt', 'r').read() new_tokens = XeroRefreshToken(old_refresh_token) xero_tenant_id = XeroTenants(new_tokens[0]) get_url = "https://api.xero.com/api.xro/2.0/Invoices/INV-" + InvoiceNumber response = requests.get(get_url, headers = { 'Authorization': 'Bearer ' + new_tokens[0], 'Xero-tenant-id': xero_tenant_id, 'Accept': 'application/json', 'Content-Type': 'application/json' }) resp = response.json() #resp["Invoices"]0['AmountDue'] print(resp["Invoices"][0]['AmountDue']) find("0228")