Steps Are as Follows
1 Variables - like Clientid, Client Secret, Grant Token
2. Generate Refresh Token
3. Generate Access Token
4. Generate Chart of Accounts
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.NH6E3JV1WF2AX2LGNKGQNGPD7OPP1C"
client_secret = "0754cfc0f7923723d8d78bedc52d8600b984ab6ea3"
organization_id = "60029233076"
#To be generated for every session
###2 Generating Grant token https://api-console.zoho.com/client/1000.53G2MOWLNZWSZAGKZJYTWOP5UEVZ9F
GrantToken = "1000.0931b9b0c854ce447524eb456b028f92.cadcda0fa49dc631f1c20ea2c1998823"
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/chartofaccounts?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 'chartofaccounts' in data_json:
# Normalize JSON data into a pandas DataFrame
df1 = pd.json_normalize(data_json['chartofaccounts'])
# Print DataFrame information and the first few rows
print(df1.info())
print(df1.head())
# Save DataFrame to CSV
df1.to_csv("ram.csv")
else:
print("Key 'chartofaccounts' not found in the JSON data")
# Close the connection
conn.close()