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 utilized. They are DataBase Persistence, File Persistence, and Custom Persistence.
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 an implementation of the Token interface object for the library to process it.
SaveToken(UserSignature user, Token token) - invoked after fetching access and refresh tokens from Zoho.
DeleteToken(UserSignature user, 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(string id, Token token) - The method to retrieve the user's token details based on unique ID.
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))
client_secret (varchar(255))
refresh_token (varchar(255))
access_token (varchar(255))
grant_token (varchar(255))
expiry_time(varchar(20))
redirect_url(varchar(255))
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:
/*
* Create an instance of DBStore.
* Host -> DataBase host name. Default "localhost"
* DatabaseName -> DataBase name. Default "zohooauth"
* UserName -> DataBase user name. Default "root"
* Password -> DataBase password. Default ""
* PortNumber -> DataBase port number. Default "3306"
* TableName -> Table Name. Default value "oauthtoken"
*/
//TokenStore tokenstore = new DBStore.Builder().Build();
//TokenStore interface
TokenStore tokenstore = new DBStore.Builder()
.Host("hostName")
.DatabaseName("dataBaseName")
.TableName("tableName")
.UserName("userName")
.Password("password")
.PortNumber("portNumber")
.Build();
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:
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:
//Parameter containing the absolute file path to store tokens
TokenStore tokenstore = new FileStore("/Users/user_name/Documents/csharp_sdk_token.txt");
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:
using System;
using System;
using Com.Zoho.API.Authenticator;
using Com.Zoho.API.Authenticator.Store;
using Com.Zoho.Crm.API;
namespace user.store
{
public class CustomStore : TokenStore
{
public CustomStore()
{
}
/// <summary></summary>
/// <param name="user">A UserSignature class instance.</param>
/// <param name="token">A Token (Com.Zoho.API.Authenticator.OAuthToken) class instance.</param>
/// <returns>A Token class instance representing the user token details.</returns>
public Token GetToken(UserSignature user, Token token)
{
// Add code to get the token
return null;
}
/// <summary></summary>
/// <param name="user">A UserSignature class instance.</param>
/// <param name="token">A Token (Com.Zoho.API.Authenticator.OAuthToken) class instance.</param>
public void SaveToken(UserSignature user, Token token)
{
// Add code to save the token
}
/// <summary></summary>
/// <param name="user">A UserSignature class instance.</param>
/// <param name="token">A Token (Com.Zoho.API.Authenticator.OAuthToken) class instance.</param>
public void DeleteToken(Token token)
{
// Add code to delete the token
}
public void GetTokens()
{
// Add code to get the all stored tokens
}
public void DeleteTokens()
{
// Add code to delete the all stored token
}
/// <summary>
/// This method is used to retrieve the user token details based on unique ID
/// </summary>
/// <param name="id">A String representing the unique ID</param>
/// <param name="token">A Token class instance representing the user token details.</param>
/// <returns>A Token class instance representing the user token details.</returns>
///
Token GetTokenById(string id, Token token)
{
// Add code to get the token using unique id
return null;
}
}
}