Token Persistence
Token persistence refers to storing and utilizing the authentication tokens that are provided by Zoho. There are three ways provided by the SDK in which persistence can be applied. They are file persistence, DB persistence (default) and Custom persistence.
Table of Contents:
Implementing OAuth Persistence
Once the application is authorized, OAuth access and refresh tokens can be used for subsequent user data requests to Zoho CRM. Hence, they need to be persisted by the client app.
The persistence is achieved by writing an implementation of the inbuilt TokenStore Abstract Base Class, which has the following callback methods.
get_token(self, user, token) - invoked before firing a request to fetch the saved tokens. This method should return implementation of Token Abstract Base Class object for the library to process it.
save_token(self, user, token) - invoked after fetching access and refresh tokens from Zoho.
delete_token(self, token) - invoked before saving the latest tokens.
get_tokens(self) - The method to retrieve all the stored tokens.
delete_tokens(self) - The method to delete all the stored tokens.
user is an instance of UserSignature class.
token is an instance of class that implements Token Abstract Base Class.
Database Persistence
If you want to use database persistence, you can use MySQL. The DB persistence mechanism is the default method.
The database name should be zohooauth.
There must be a table oauthtokens with columns
id(int(11))
user_mail (varchar(255))
client_id (varchar(255))
refresh_token (varchar(255))
grant_token (varchar(255))
access_token (varchar(255))
expiry_time(varchar(20))
The Database persistence requires the following libraries :
MySQL Query
create table oauthtoken(id int(11) not null auto_increment, user_mail varchar(255) not null, client_id varchar(255), refresh_token varchar(255), access_token varchar(255), grant_token varchar(255), expiry_time varchar(20), primary key (id));
alter table oauthtoken auto_increment = 1;
Here is the code to create a DBStore object:
/*
from zcrmsdk.src.com.zoho.api.authenticator.store import DBStore
"""
DBStore takes the following parameters
1 -> DataBase host name. Default value "localhost"
2 -> DataBase name. Default value "zohooauth"
3 -> DataBase user name. Default value "root"
4 -> DataBase password. Default value ""
5 -> DataBase port number. Default value "3306"
"""
store = DBStore()
store = DBStore(host='host_name', database_name='database_name', user_name='user_name', password='password', port_number='port_number')
File Persistence
In case of file persistence, you can set up persistence the tokens in the local drive, and provide the absolute file path in the FileStore object. This file must contain the following:
user_mail
client_id
refresh_token
access_token
grant_token
expiry_time
Here is the code to create a FileStore object:
from zcrmsdk.src.com.zoho.api.authenticator.store import FileStore
"""
FileStore takes the following parameter
1 -> Absolute file path of the file to persist tokens
"""
store = FileStore(file_path='/Users/username/Documents/python_sdk_tokens.txt')
Custom Persistence
To use Custom Persistence, you must implement the Abstract Base Class TokenStore (zcrmsdk/src/com/zoho/api/authenticator/store/token_store) and override the methods.
Here is the code:
namespace store;
from zcrmsdk.src.com.zoho.api.authenticator.store import TokenStore
class CustomStore(TokenStore):
def __init__(self):
pass
def get_token(self, user, token):
"""
Parameters:
user (UserSignature) : A UserSignature class instance.
token (Token) : A Token (zcrmsdk.src.com.zoho.api.authenticator.OAuthToken) class instance
"""
# Add code to get the token
return None
def save_token(self, user, token):
"""
Parameters:
user (UserSignature) : A UserSignature class instance.
token (Token) : A Token (zcrmsdk.src.com.zoho.api.authenticator.OAuthToken) class instance
"""
# Add code to save the token
def delete_token(self, token):
"""
Parameters:
token (Token) : A Token (zcrmsdk.src.com.zoho.api.authenticator.OAuthToken) class instance
"""
# Add code to delete the token
def get_tokens(self):
"""
Returns:
list: List of stored tokens
"""
# Add code to get the all stored tokens
def delete_tokens(self):
# Add code to delete the all stored tokens