Token Persistence
New SDKs Released!
We have released new versions of our SDKs, now supporting the latest version of our APIs. Explore them here.
Token persistence refers to storing and utilizing the authentication tokens that are provided by Zoho.
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(UserSignature user, Token token) - invoked before firing a request to fetch the saved tokens. This method should return implementation Token interface object for the library to process it.
- saveToken(UserSignature user, 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.
There are three ways provided by the SDK in which you can achieve persistence. They are:
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))
- access_token (varchar(255))
- refresh_token (varchar(255))
- grant_token (varchar(255))
- expiry_time (varchar (20))
Here is the 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:
/*
* 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 tokenstore = new DBStore();
//TokenStore interface
TokenStore tokenstore = new DBStore("hostName", "dataBaseName", "userName", "password", "portNumber");
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:
//Parameter containing the absolute file path to store tokens
TokenStore tokenstore = new FileStore("/Users/username/Documents);
Custom Persistence
To use Custom Persistence, you must implement the TokenStore interface (com.zoho.api.authenticator.store.TokenStore) and override the methods.
Here is the code:
package user.store;
import com.zoho.api.authenticator.Token;
import com.zoho.api.exception.SDKException;
import com.zoho.crm.api.UserSignature;
import com.zoho.api.authenticator.store.TokenStore;
public class CustomStore implements TokenStore
{
/**
* @param user A UserSignature class instance.
* @param token A Token (com.zoho.api.authenticator.OAuthToken) class instance.
* @return A Token class instance representing the user token details.
* @throws SDKException if any problem occurs.
*/
@Override
public Token getToken(UserSignature user, Token token) throws SDKException
{
// Add code to get the token
return null;
}
/**
* @param user A UserSignature class instance.
* @param token A Token (com.zoho.api.authenticator.OAuthToken) class instance.
* @throws SDKException if any problem occurs.
*/
@Override
public void saveToken(UserSignature user, Token token) throws SDKException
{
// Add code to save the token
}
/**
* @param token A Token (com.zoho.api.authenticator.OAuthToken) class instance.
* @throws SDKException if any problem occurs.
*/
@Override
public void deleteToken(Token token) throws SDKException
{
// Add code to delete the token
}
@Override
public List<Token> getTokens() throws SDKException
{
// Add code to get the all stored tokens
}
@Override
public void deleteTokens() throws SDKException
{
// Add code to delete the all stored token
}
}