Before we go into detail about the SDK initialization process, ensure that you have the following prerequisites:
Configuration | Description |
environment
mandatory
| Specify the Zoho CRM domain and environment to make API calls to. Options include USDataCenter, EUDataCenter, INDataCenter, CNDataCenter, AUDataCenter, with environments like PRODUCTION(), DEVELOPER(), SANDBOX(). |
token mandatory | Contains user authentication details. Used to create an OAuthToken instance with client credentials and tokens. Supports different authentication flows: Grant Token Flow: Uses a grant token to generate and persist access and refresh tokens. Refresh Token Flow: Uses a refresh token to generate and persist access tokens. Access Token Flow: Directly uses an access token for making API calls. ID Flow : Uses the ID from the persisted token file/DB to make API calls. This method is applicable only after the SDK has been initialized at least once using a grant token, access token, or refresh token. It is not valid for the initial setup but can simplify subsequent operations by bypassing the need for other token details. |
| Manages token persistence, which is the storage and retrieval of authentication tokens. Options include: DBStore: Stores tokens in a database (e.g., MySQL). FileStore: Stores tokens in a file. Custom Store: Allows the implementation of custom storage logic. If not specified, defaults to file storage in the current working directory. |
| Configures SDK logging. Allows setting log level (e.g., INFO, DEBUG, ERROR) and file path for SDK operation logs. Helps in troubleshooting and monitoring operations. |
| Contains additional SDK-wide settings:
auto_refresh_fields: Enables/disables automatic refreshing of module fields. If set to true, the SDK will refresh modules and fields metadata every hour. If set to false, the metadata should be manually refreshed. pick_list_validation: Enables/disables validation of picklist field values. If set to true, the SDK checks user inputs against the defined picklist values. Invalid inputs, i.e., values not present in the picklist, will cause the SDK to throw an error. connect_timeout: Sets the maximum time to wait for connection establishment. read_timeout: Sets the maximum time to wait for data retrieval. |
| Specify the directory path for storing module field information cache. |
Initializing Python SDK for Self-Clients
To initialize the SDK for a self-client:
1. Register the Client: In the Zoho API Console, create a self-client by navigating to the Self-Client section. This client is used for applications accessing only your own CRM data.
2. Generate Grant Token: After registering the client, manually generate a grant token from the API Console. Specify the necessary scopes, such as ZohoCRM.modules.ALL, based on the data you need to access.
Note: The grant token is valid for a short duration, typically 3–10 minutes, and is used to generate access and refresh tokens, using which one can access the CRM data.
3. Install the Python SDK
Install the Zoho CRM Python SDK in your Python project. The latest SDK version supports V7 of Zoho CRM APIs.
4. Exchange Grant Token for Access and Refresh Tokens
Use the SDK in your code to exchange the grant token for access and refresh tokens. The SDK provides built-in methods to handle this process, and it will automatically manage token generation and persistence after initialization.
5. Access CRM Data:
With the tokens in place, your backend application can access and interact with Zoho CRM data programmatically, enabling tasks like data synchronization.
Here is a sample code for initializing Python SDK for a self-client.
Initializing Python SDK for Server-Based Clients
Before initializing the SDK for a server-based client, you must register your application in the Zoho API Console.
After registering the client, you can proceed with initializing the SDK for the server-based client. When using a server-based client with the Zoho CRM Python SDK, you have two options for handling authentication:
- Generate the grant token manually and let the SDK manage the rest, including access token generation and refresh operations.
- Write custom code to handle the entire OAuth process, from grant token generation to subsequent API calls. This approach is commonly used in server-based applications where automation and granular control over the flow are essential.
In the
sample code, we demonstrate a complete implementation of initialization for a server-based client. The code includes generating the grant token for the required scopes, initializing the SDK with the tokens, and fetching records from Zoho CRM.
The code defines an HTTP server that automates the OAuth process and interacts with Zoho CRM APIs. Here's a breakdown of its key functionalities:
Grant Token Generation:
When the user accesses
http://127.0.0.1:8081/login, the server redirects them to Zoho's authorization page with the necessary OAuth scopes that is defined in the code.
After successful authorization, Zoho redirects the user to the redirect URL configured for the client in the API console, with a grant token and other parameters. The code parses this redirected URL to extract the grant token and the associated location.
SDK Initialization:
Using the extracted grant token, the SDK generates access and refresh tokens. These tokens are securely stored in a data store (FileStore in this case) using a unique identifier that corresponds to the user-org combination.