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 custom persistence, file persistence, and DB persistence (default).
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 interface, which has the following callback methods.
getToken(user :UserSignature, token :Token) - invoked before firing a request to fetch the saved tokens. This method should return an implementation of the Token interface object for the library to process it.
saveToken(user:UserSignature, token :Token) - invoked after fetching access and refresh tokens from Zoho.
deleteToken(token :Token) - invoked before saving the latest tokens.
getTokens() - The method to retrieve all the stored tokens.
deleteTokens() - The method to delete all the stored tokens.
getTokenById(id: String, token :Token) - The method to retrieve the user's token details based on unique ID.
Database Persistence
In case the user prefers to use the default DataBase persistence, MySQL can be used.
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))
client_secret(varchar(255))
refresh_token(varchar(255))
access_token(varchar(255))
grant_token(varchar(255))
expiry_time(varchar(20))
redirect_url(varchar(255))
Custom database name and table name can be set in DBStore instance
MySQL Query
CREATE TABLE oauthtoken (
id varchar(255) NOT NULL,
user_mail varchar(255) NOT NULL,
client_id varchar(255),
client_secret varchar(255),
refresh_token varchar(255),
access_token varchar(255),
grant_token varchar(255),
expiry_time varchar(20),
redirect_url varchar(255),
primary key (id)
);
Here is the code to create a DBStore object:
/*
/*
* 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"
*/
//TokenStore interface
val tokenstore = new DBStore.Builder()
.host("hostName")
.databaseName("dataBaseName")
.tableName("tableName")
.userName("userName")
.password("password")
.portNumber("portNumber")
.build
File Persistence
In case of default File Persistence, the user can persist tokens in the local drive, by providing the the absolute file path to the FileStore object.
The file contains:
- id
- user_mail
- client_id
- client_secret
- refresh_token
- access_token
- grant_token
- expiry_time
- redirect_url
Here is the code to create a FileStore object:
/*
import com.zoho.api.authenticator.store.FileStore
//Parameter containing the absolute file path to store tokens
var tokenstore = new FileStore("/Users/user_name/Documents/scala_sdk_token.txt")
Custom Persistence
To use Custom Persistence, the user must extend TokenStore interface(com.zoho.api.authenticator.store.TokenStore) and override the methods.
Here is the code:
using System;
import com.zoho.api.authenticator.Token
import com.zoho.crm.api.exception.SDKException
import com.zoho.crm.api.UserSignature
import com.zoho.api.authenticator.OAuthToken
import scala.collection.mutable.ArrayBuffer
import com.zoho.crm.api.UserSignature
import com.zoho.api.authenticator.store.TokenStore
class CustomeStore extends TokenStore
{
/**
* This method is used to get user token details.
*
* @param user A User class instance.
* @param token A Token class instance.
* @return A Token class instance representing the user token details.
* @throws SDKException SDKException
*/
override def getToken(user :UserSignature, token :Token) :Token
/**
* This method is used to store user token details.
*
* @param user A User class instance.
* @param token A Token class instance.
* @throws SDKException SDKException
*/
override def saveToken(user :UserSignature, token :Token)
/**
* This method is used to delete user token details.
* @param token A Token class instance.
* @throws SDKException SDKException
*/
override def deleteToken(token :Token)
/**
* The method to retrieve all the stored tokens.
*
* @throws SDKException if any problem occurs.
*/
@throws[SDKException]
override def getTokens: ArrayBuffer[OAuthToken]
/**
* The method to delete all the stored tokens.
*
* @throws SDKException if any problem occurs.
*/
@throws[SDKException]
override def deleteTokens(): Unit
/**
* This method is used to get user token details by id.
*
* @param id A String
* @param token A Token class instance.
* @return A Token class instance representing the user token details.
* @throws SDKException if any problem occurs.
*/
override def getTokenById(id: String, token: Token): Token
}