Jupyter Notebook调用Quickbooks Online API授权失败求简易认证方案
Hey there! I’ve worked with QuickBooks Online (QBO) API in Jupyter Notebooks before, so let me break this down clearly for you.
First off, I need to clarify: QBO API does NOT support a simple quickbooks.apikey = [API KEY] style authentication. It relies entirely on OAuth 2.0 for securing access to sensitive financial data, which is a bit more involved but non-negotiable for compliance and security reasons.
Here’s how to get your Jupyter Notebook authenticated properly:
Step 1: Grab Your Developer Credentials
Before writing any code, you’ll need to:
- Sign up for a QuickBooks Developer account
- Create a new app to generate your
Client IDandClient Secret - Set a local redirect URI (something like
http://localhost:8000/works great for testing)
Step 2: Use the quickbooks-python Library
The most straightforward way to handle OAuth and API calls in Python is with the popular quickbooks library. Install it directly in your notebook first:
!pip install quickbooks
Step 3: Walk Through the OAuth Flow in Jupyter
Since Jupyter runs in a browser, you can set up a local callback to capture the authorization code. Here’s a working example:
from quickbooks import QuickBooks from quickbooks.auth import OAuth2Session import webbrowser # Replace these with your actual credentials CLIENT_ID = "YOUR_CLIENT_ID" CLIENT_SECRET = "YOUR_CLIENT_SECRET" REDIRECT_URI = "http://localhost:8000/" # Initialize the OAuth session oauth_session = OAuth2Session(CLIENT_ID, redirect_uri=REDIRECT_URI) # Generate the authorization URL auth_url, _ = oauth_session.authorization_url('https://appcenter.intuit.com/connect/oauth2') # Open the auth URL in your browser to grant access to your QBO account webbrowser.open(auth_url) # After granting access, you'll be redirected to your URI with a code in the address bar # Copy that code and paste it when prompted auth_code = input("Paste the authorization code from the URL here: ") # Fetch your access token oauth_session.fetch_token( 'https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer', code=auth_code, client_secret=CLIENT_SECRET ) # Initialize the QBO client with your authenticated session qb_client = QuickBooks( auth_client=oauth_session, company_id="YOUR_COMPANY_ID" # Find this in your QBO account settings )
Step 4: Test a Sample Data Pull
Once authenticated, you can start fetching data like this:
from quickbooks.objects.customer import Customer # Fetch all customers in your QBO account customers = Customer.all(qb=qb_client) for customer in customers: print(f"Customer: {customer.DisplayName}, Email: {customer.PrimaryEmailAddr.Address if customer.PrimaryEmailAddr else 'N/A'}")
Why No Simple API Key?
QBO’s OAuth 2.0 flow ensures users explicitly grant your application permission to access their financial data—this is a mandatory security requirement for platforms handling sensitive accounting information. There’s no way around this flow for production or even testing with real account data.
For safer testing, I recommend using the QuickBooks Sandbox environment to avoid touching live financial records!
内容的提问来源于stack exchange,提问作者jonplaca

