Zoho Basic Connect For Chart of Accounts

Python, Zohobooks , API
Post Reply
admin
Site Admin
Posts: 119
Joined: Fri May 10, 2024 2:46 pm
Location:

Zoho Basic Connect For Chart of Accounts

Post by admin »

Code: Select all

# 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()
Mpahalith
Posts: 4
Joined: Mon Mar 17, 2025 3:57 am
Location: Chennai
Region name: Southern

Re: Zoho Basic Connect For Listing the Currencies

Post by Mpahalith »

Code: Select all

###1 Generating Client id, Secret, https://api-console.zoho.com/client/1000.53G2MOWLNZWSZAGKZJYTWOP5UEVZ9F
#https://www.zoho.com/books/api/v3/introduction/

import requests
import json
import pandas as pd

client_id = "1000.GHH8B2HOTDXITI8JAND4AEJUQM3AET"
client_secret = "7ea93730fc6c7b5a8ba4f65d591c7e3339cc5c35b0"
organization_id = "60039132653"


#To be generated for every session

###2 Generating  Grant token https://api-console.zoho.com/client/1000.53G2MOWLNZWSZAGKZJYTWOP5UEVZ9F

GrantToken = "1000.226b62b853c49b00fdeeee3a1fe136c2.265a7069032282ed4ea7b5acf3de8c26"
scope  = "ZohoBooks.FullAccess.all"

##3 Generating Refresh Token Apicall

GRTATurl = "https://accounts.zoho.in/oauth/v2/token?grant_type=authorization_code&code=" + GrantToken + "&client_id="+ client_id + "&client_secret=" + client_secret
print(GRTATurl)

response = requests.post(GRTATurl)
data = json.loads(response.text)
refresh_token = data['refresh_token']
print("refresh_token:", refresh_token)

##3 Generating Access Token Apicall

import http.client

Gt = "https://accounts.zoho.in/oauth/v2/token?refresh_token=" + refresh_token  + "&client_id="+ client_id + "&client_secret=" + client_secret + "&grant_type=refresh_token"
response1 = requests.post(Gt)
data1 = json.loads(response1.text)
access_token = data1['access_token']
print("access_token:", access_token)

##4 Actual Post/ Get request to books
#Variable ORgid = from organisation
conn = http.client.HTTPSConnection("www.zohoapis.in")
headers = {'Authorization': "Zoho-oauthtoken " + access_token}
conn.request("GET", "/books/v3/settings/currencies?organization_id=" + organization_id, headers=headers)

# Get response from the server
res = conn.getresponse()
data = res.read()

# Decode JSON data
data_json = json.loads(data.decode('utf-8'))

# Extract 'chartofaccounts' data if it exists in the JSON
if 'currencies' in data_json:
    # Normalize JSON data into a pandas DataFrame
    df1 = pd.json_normalize(data_json['currencies'])
    # Print DataFrame information and the first few rows
    print(df1.info())
    print(df1.head())

    # Save DataFrame to CSV
    df1.to_csv("currencies.csv")
else:
    print("Key 'currencies' not found in the JSON data")

# Close the connection
conn.close()
Mpahalith
Posts: 4
Joined: Mon Mar 17, 2025 3:57 am
Location: Chennai
Region name: Southern

Re: Zoho Basic Connect For list of invoice

Post by Mpahalith »

Code: Select all

###1 Generating Client id, Secret, https://api-console.zoho.com/client/1000.53G2MOWLNZWSZAGKZJYTWOP5UEVZ9F
#https://www.zoho.com/books/api/v3/introduction/

import requests
import json
import pandas as pd

client_id = "1000.GHH8B2HOTDXITI8JAND4AEJUQM3AET"
client_secret = "7ea93730fc6c7b5a8ba4f65d591c7e3339cc5c35b0"
organization_id = "60039132653"


#To be generated for every session

###2 Generating  Grant token https://api-console.zoho.com/client/1000.53G2MOWLNZWSZAGKZJYTWOP5UEVZ9F

GrantToken = "1000.5bbeb73107dd54d2edc9b0ed645e3243.30423ffdbe19c5a4d003777c6cc0c9e8"
scope  = "ZohoBooks.FullAccess.all"

##3 Generating Refresh Token Apicall

GRTATurl = "https://accounts.zoho.in/oauth/v2/token?grant_type=authorization_code&code=" + GrantToken + "&client_id="+ client_id + "&client_secret=" + client_secret
print(GRTATurl)

response = requests.post(GRTATurl)
data = json.loads(response.text)
refresh_token = data['refresh_token']
print("refresh_token:", refresh_token)

##3 Generating Access Token Apicall

import http.client

Gt = "https://accounts.zoho.in/oauth/v2/token?refresh_token=" + refresh_token  + "&client_id="+ client_id + "&client_secret=" + client_secret + "&grant_type=refresh_token"
response1 = requests.post(Gt)
data1 = json.loads(response1.text)
access_token = data1['access_token']
print("access_token:", access_token)

##4 Actual Post/ Get request to books
#Variable ORgid = from organisation
conn = http.client.HTTPSConnection("www.zohoapis.in")
headers = {'Authorization': "Zoho-oauthtoken " + access_token}
conn.request("GET", "/books/v3/invoices?organization_id=" + organization_id, headers=headers)

# Get response from the server
res = conn.getresponse()
data = res.read()

# Decode JSON data
data_json = json.loads(data.decode('utf-8'))

# Extract 'invoices' data if it exists in the JSON
if 'invoices' in data_json:
    # Normalize JSON data into a pandas DataFrame
    df1 = pd.json_normalize(data_json['invoices'])
    # Print DataFrame information and the first few rows
    print(df1.info())
    print(df1.head())

    # Save DataFrame to CSV
    df1.to_csv("invoices.csv")
else:
    print("Key 'ìnvoices' not found in the JSON data")

# Close the connection
conn.close()
Mpahalith
Posts: 4
Joined: Mon Mar 17, 2025 3:57 am
Location: Chennai
Region name: Southern

Re: Zoho Basic Connect For list of journals

Post by Mpahalith »

Code: Select all

###1 Generating Client id, Secret, https://api-console.zoho.com/client/1000.53G2MOWLNZWSZAGKZJYTWOP5UEVZ9F
#https://www.zoho.com/books/api/v3/introduction/

import requests
import json
import pandas as pd

client_id = "1000.GHH8B2HOTDXITI8JAND4AEJUQM3AET"
client_secret = "7ea93730fc6c7b5a8ba4f65d591c7e3339cc5c35b0"
organization_id = "60039132653"


#To be generated for every session

###2 Generating  Grant token https://api-console.zoho.com/client/1000.53G2MOWLNZWSZAGKZJYTWOP5UEVZ9F

GrantToken = "1000.33847f0e4d7a99355e59a85545c5621f.ae5563b51ccd08edec539a8db8b83c17"
scope  = "ZohoBooks.FullAccess.all"

##3 Generating Refresh Token Apicall

GRTATurl = "https://accounts.zoho.in/oauth/v2/token?grant_type=authorization_code&code=" + GrantToken + "&client_id="+ client_id + "&client_secret=" + client_secret
print(GRTATurl)

response = requests.post(GRTATurl)
data = json.loads(response.text)
refresh_token = data['refresh_token']
print("refresh_token:", refresh_token)

##3 Generating Access Token Apicall

import http.client

Gt = "https://accounts.zoho.in/oauth/v2/token?refresh_token=" + refresh_token  + "&client_id="+ client_id + "&client_secret=" + client_secret + "&grant_type=refresh_token"
response1 = requests.post(Gt)
data1 = json.loads(response1.text)
access_token = data1['access_token']
print("access_token:", access_token)

##4 Actual Post/ Get request to books
#Variable ORgid = from organisation
conn = http.client.HTTPSConnection("www.zohoapis.in")
headers = {'Authorization': "Zoho-oauthtoken " + access_token}
conn.request("GET", "/books/v3/journals?organization_id=" + organization_id, headers=headers)

# Get response from the server
res = conn.getresponse()
data = res.read()

# Decode JSON data
data_json = json.loads(data.decode('utf-8'))

# Extract 'journals' data if it exists in the JSON
if 'journals' in data_json:
    # Normalize JSON data into a pandas DataFrame
    df1 = pd.json_normalize(data_json['journals'])
    # Print DataFrame information and the first few rows
    print(df1.info())
    print(df1.head())

    # Save DataFrame to CSV
    df1.to_csv("journals.csv")
else:
    print("Key 'journals' not found in the JSON data")

# Close the connection
conn.close()
Post Reply