Page 1 of 1

Creation of ledgers in Zoho

Posted: Thu May 23, 2024 2:23 am
by admin

Code: Select all

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
import json
import pandas as pd

import http.client
x = 1

while x < 4:

    Acc = "Sample in zoho asd " + str(x)
    conn = http.client.HTTPSConnection("www.zohoapis.in")
    payload = json.dumps({
        "account_name": Acc,
        "account_code": 45741741145 + x,
        "account_type": "other_current_liability",
        #"currency_id": currency_id_inr,
        "description": "Testing. V 2.0 ",
      })

#print (payload)
    headers = {'Authorization': "Zoho-oauthtoken " + access_token, 'content-type': "application/json"}

    conn.request("POST", "/books/v3/chartofaccounts?organization_id=" + organization_id , payload, headers=headers)

    res = conn.getresponse()
    data = res.read()

    print(data.decode("utf-8"))
    x += 1

The above code runs after authentication

if you have not done any authentication earlier by generating the Grant, Refresh, Access token then you have to do that first.

Let me put a code stand alone for this below


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.d81f6d165ab36406bfa076d8d26fb767.d060980770012a42693dceee6b5b422a"
scope  = "ZohoBooks.FullAccess.all"

##3 Generating Refresh Token Apicall


##3 Generating Access Token Apicall



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
import json
import pandas as pd

import http.client
x = 1

while x < 4:

    Acc = "Sample in zoho asd " + str(x)
    conn = http.client.HTTPSConnection("www.zohoapis.in")
    payload = json.dumps({
        "account_name": Acc,
        "account_code": 45741741145 + x,
        "account_type": "other_current_liability",
        #"currency_id": currency_id_inr,
        "description": "Testing. V 2.0 ",
      })

#print (payload)
    headers = {'Authorization': "Zoho-oauthtoken " + access_token, 'content-type': "application/json"}

    conn.request("POST", "/books/v3/chartofaccounts?organization_id=" + organization_id , payload, headers=headers)

    res = conn.getresponse()
    data = res.read()

    print(data.decode("utf-8"))
    x += 1

You need to convert the json data to dataframe and then to excel / csv