pythondjangocrmzoho

Zoho CRM API: Python request-based POST or GET authentication + insertion of contacts


The Task

A Django application that allows users to sign up and once the user clicks on the account activation link, Zoho CRM is receiving the data and a contact is created in the CRM section.

The Problem

I am currently working on an absolute masterpiece - the ZOHO API. I am struggling to set up the native Python code that uses POST/GET requests.

Regarding the zcrmsdk 3.0.0, I have completely given up on this solution unless somebody can provide a fully functional example. The support simply blames my code.

The documentation I consulted:

Since the post request in postman API works fine I do not understand why it does not work in Python code.

My approach

  1. Generate an self-client API code on: https://api-console.zoho.com/
  2. Insert that code on Postman and retrieve the access or refresh token
  3. Use this access token in an add_user_contact function that is defined in the documentation It works! Response is success and it is in Zoho CRM

The permsissions scope I am using is: ZohoCRM.modules.contacts.ALL, ZohoCRM.users.ALL, ZohoCRM.modules.deals.ALL, ZohoCRM.modules.attachments.ALL, ZohoCRM.settings.ALL, AAAserver.profile.ALL

Pictures of Postman POST request

Header:

the header

Body:

the body content

Expected response:

the expected response

My own code

def authenticate_crm():

    """
    access to response object id:
    response_object.get('data')[0].get('details').get('id')
    """

    url = 'https://accounts.zoho.com/oauth/v2/token'

    headers = {
        'Content-Type': 'application/x-www-form-urlencoded'
    }

    # one time self-client token here -
    request_body = {
        "code": "1000.aa8abec144835ab79b8f9141fa1fb170.8ab194e4e668b8452847c7080c2dd479",
        "redirect_uri": "http://example.com/yourcallback",
        "client_id": "1000.H95VDM1H9KCXIADGF05E0E1XSVZKFQ",
        "client_secret": "290e505ec52685fa62a640d874e6560f2fc8632e97",
       " grant_type": "authorization_code"
    }

    response = requests.post(url=url, headers=headers, data=json.dumps(request_body).encode('utf-8'))
    if response is not None:
        print("HTTP Status Code : " + str(response.status_code))
        print(response.json())

I am essentially struggling to convert the Postman API request to a Python request to get the token as part of the workflow. What am I doing wrong here?

The documentation states: Note: For security reasons, pass the below parameters in the body of your request as form-data. (access-refresh link) but passing it in Postman as form-data breaks the call completely.

According to their own documentation (which is convoluted, contradictory and full of outdated screenshots) the authentication key is needed only once.

Once the request from above runs, I would take the response in the third image and use the refresh key to add the contact.

I am also open to a solution with the SDK 3.0.0, if anybody can help.


Solution

  • I solved it!

    I have changed this line:

    response = requests.post(url=url, headers=headers, data=json.dumps(request_body).encode('utf-8'))
    

    to this and added some return statement:

    payload =     '1000.6d9411488dcac999f02304d1f7843ab2.e14190ee4bae175debf00d2f87143b19&'     \
              'redirect_uri=http%3A%2F%2Fexample.com%2Fyourcallback&' \
              'client_id=1000.H95VDM1H9KCXIADGF05E0E1XSVZKFQ&' \
             'client_secret=290e505ec52685fa62a640d874e6560f2fc8632e97&'\
              'grant_type=authorization_code'
    
    response = requests.request(method="POST", url=url, headers=headers,     data=payload)
    if response is not None:
        print("HTTP Status Code : " + str(response.status_code))
        # print(response.text)
        print(response.json())
        # catch access and refresh token
    at = response.json().get('access_token')
    rt = response.json().get('refresh_token')
    
    return at, rt
    

    I do not understand why that is different but that fixed it and I could retrieve keys from ZOHO.