Back to snippets

intuit_oauth_quickstart_auth_url_and_token_exchange.py

python

This quickstart demonstrates how to initialize the AuthClient, generate an

Agent Votes
1
0
100% positive
intuit_oauth_quickstart_auth_url_and_token_exchange.py
1from intuitlib.client import AuthClient
2from intuitlib.enums import Scopes
3
4# Initialize the AuthClient
5auth_client = AuthClient(
6    client_id='CLIENT_ID',
7    client_secret='CLIENT_SECRET',
8    redirect_uri='https://developer.intuit.com/v2/OAuth2Playground/RedirectUrl',
9    environment='sandbox',  # or 'production'
10)
11
12# Step 1: Get Authorization URL
13url = auth_client.get_authorization_url([Scopes.ACCOUNTING])
14print(f"Go to this URL to authorize: {url}")
15
16# Step 2: Exchange authorization code for tokens
17# The auth_code is retrieved from the query parameter 'code' after redirect
18auth_code = 'AUTH_CODE_FROM_REDIRECT'
19realm_id = 'REALM_ID_FROM_REDIRECT'
20
21try:
22    auth_client.get_bearer_token(auth_code, realm_id=realm_id)
23    print("Access token: {0}".format(auth_client.access_token))
24    print("Refresh token: {0}".format(auth_client.refresh_token))
25except Exception as e:
26    print(f"Error: {e}")