# Import required libraries for making HTTP requests, handling JSON data, and data manipulation
import requests # Library for making HTTP requests
import json # Library for JSON data parsing
import pandas as pd # Library for data manipulation and analysis
import http.client # Library for making HTTP connections
# Zoho API Credentials
# Client ID from Zoho API Console - used for authentication
client_id = "1000.NH6E3JV1WF2AX2LGNKGQNGPD7OPP1C"
# Client Secret from Zoho API Console - used for authentication
client_secret = "0754cfc0f7923723d8d78bedc52d8600b984ab6ea3"
# Organization ID specific to the Zoho Books account
organization_id = "60039067717"
# Grant Token - a temporary authorization code used to obtain refresh token
# This token is generated manually through Zoho's OAuth authorization process
GrantToken = "1000.616a47ac75c01adcc2b6c9526b6265d7.cc9f9abe26687eb1c90b3864386a858b"
# Scope defines the level of access requested for the API
scope = "ZohoBooks.FullAccess.all"
# Construct URL to exchange Grant Token for Refresh Token
# This is the first step in the OAuth 2.0 authentication flow
GRTATurl = "https://accounts.zoho.in/oauth/v2/token?grant_type=authorization_code&code=" + GrantToken + "&client_id="+ client_id + "&client_secret=" + client_secret
# Print the constructed URL for debugging purposes
print(GRTATurl)
# Send POST request to obtain refresh token
response = requests.post(GRTATurl)
# Parse the JSON response
data = json.loads(response.text)
# Extract the refresh token from the response
refresh_token = data['refresh_token']
# Print the refresh token
print("refresh_token:", refresh_token)
# Construct URL to exchange Refresh Token for Access Token
# This is the second step in the OAuth 2.0 authentication flow
Gt = "https://accounts.zoho.in/oauth/v2/token?refresh_token=" + refresh_token + "&client_id="+ client_id + "&client_secret=" + client_secret + "&grant_type=refresh_token"
# Send POST request to obtain access token
response1 = requests.post(Gt)
# Parse the JSON response
data1 = json.loads(response1.text)
# Extract the access token from the response
access_token = data1['access_token']
# Print the access token
print("access_token:", access_token)
# Establish an HTTPS connection to Zoho Books API
conn = http.client.HTTPSConnection("www.zohoapis.in")
# Prepare Authorization header with the access token
headers = {'Authorization': "Zoho-oauthtoken " + access_token}
# Send GET request to retrieve Chart of Accounts, specifying the organization ID
conn.request("GET", "/books/v3/chartofaccounts?organization_id=" + organization_id, headers=headers)
# Get the response from the server
res = conn.getresponse()
# Read the response data
data = res.read()
# Decode the response data from bytes to UTF-8 and parse as JSON
data_json = json.loads(data.decode('utf-8'))
# Check if 'chartofaccounts' key exists in the JSON response
if 'chartofaccounts' in data_json:
# Use pandas to normalize the JSON data into a DataFrame
# This flattens nested JSON structures into a tabular format
df1 = pd.json_normalize(data_json['chartofaccounts'])
# Print information about the DataFrame (columns, data types, non-null counts)
print(df1.info())
# Display the first few rows of the DataFrame
print(df1.head())
# Save the DataFrame to a CSV file for further analysis or backup
df1.to_csv("ram.csv")
else:
# Print a message if the expected key is not found in the JSON response
print("Key 'chartofaccounts' not found in the JSON data")
# Close the HTTP connection
conn.close()