ej2 / python-quickbooks

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

AuthClientError #272

Closed bschembri-UoM closed 2 years ago

bschembri-UoM commented 2 years ago

Am trying to use the library but the bellow error; intuitlib.exceptions.AuthClientError: HTTP status 400, error message: b'{"error_description":"Incorrect or invalid refresh token","error":"invalid_grant"}', intuit_tid 1-6283b427-062f1d215a21a12048b92b5e at time Tue, 17 May 2022 14:41:43 GMT

This is the piece of code I have to be able to get some understanding of what and how I can do things with the library;

auth_client = AuthClient(
        client_id=client_id,
        client_secret=client_secret,
        # access_token='ACCESS_TOKEN',  # If you do not pass this in, the Quickbooks client will call refresh and get a new access token.
        environment=environment,
        redirect_uri=redirect_uri,
    )
client = QuickBooks(
        auth_client=auth_client,
        refresh_token='REFRESH_TOKEN',
        company_id=company_id,
    )

from quickbooks.objects.customer import Customer
customers = Customer.all(qb=client)
json_data = customers.to_json()

print(json_data)

I have commented the access_token part with the understanding that the Quickbooks client will get a new access token. I also tried with the access_token set but am not sure how to get the actual access token to be able to pass this to the AuthClient.

What am I missing and how can this be resolved?

Thank you in advance.

dmitriyshashkin commented 2 years ago

@bschembri-UoM Most likely you're using an old value of the refresh token.

  1. Obtain a new refresh token (you'll have to go through the whole oauth flow one more time)
  2. Next time you create a QuickBooks instance make sure you save client.refresh_token to some permament storage

Notice that once QuickBooks class instance is created the new value of the refresh token is saved as the intance property https://github.com/ej2/python-quickbooks/blob/master/quickbooks/client.py#L84

It might not be quite obvious, but the refresh token changes it's value once a day. When you request a new access token, API might return a new value of the refresh token as well. Next time you'll need this new value to refresh access token. If you fail to do that and use the original value of the refresh token instead you'll get the "invalid grant" error.

Here's the docs section that you might find helpful https://help.developer.intuit.com/s/article/Validity-of-Refresh-Token