ej2 / python-quickbooks

A Python library for accessing the Quickbooks API.
MIT License
407 stars 195 forks source link

QuickBooks Objects AuthorizationException in Production App (Works fine in Sandbox) #369

Open EricZiola opened 1 week ago

EricZiola commented 1 week ago

Running this code from the documentation works perfectly in the sandbox, but throws an AuthorizationException when running in production workspace (I have verified that the client is authenticated properly) :

from quickbooks import QuickBooks
from quickbooks.objects import Vendor

# Initialize a QuickBooks client
client = QuickBooks(
    auth_client=auth_client,
    refresh_token=refresh_token,
    company_id=os.getenv("REALM_ID")
)

# Retrieve customers
customers = Customer.all(qb=client)
for customer in customers:
    print(customer)

AuthorizationException: QB Auth Exception 401: Application authentication failed <?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><IntuitResponse xmlns=\"http://schema.intuit.com/finance/v3\" time=\"2024-11-21T08:19:06.093-08:00\"><Fault type=\"AuthenticationFault\"><Error code=\"130\">Accessing Wrong ClusterWrongClusterError: -11017-You have accessed the wrong server. You will now be redirected. , statusCode: 401"

This code works fine when I authenticate my client object in the sandbox workspace, all good there.

The code throws the above AuthorizationException when I authenticate the client to the production workspace. I have confirmed that the client authorization is functioning properly by using the same access token to successfully perform a GET request manually to confirm authorization is functioning.

The following code receives a valid response from the server, confirming client authentication in production:

if ENVIRONMENT == "sandbox":    
    qb_base_url = "https://sandbox-quickbooks.api.intuit.com"
else:
    qb_base_url = "https://quickbooks.api.intuit.com"

headers = {
    "Authorization": f"Bearer {auth_client.access_token}",
    "Accept": "application/json"
}

select_statement = "SELECT * FROM Customer STARTPOSITION 1 MAXRESULTS 10"
select_url = f"{qb_base_url}/v3/company/{realm_id}/query?query={select_statement}"
response = requests.get(select_url, headers=headers, timeout=30)
pprint(response.json())