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.
Implementing OAuth Persistence
saveToken($user, $token) - invoked after fetching access and refresh tokens from Zoho.
deleteToken( $token) - invoked before saving the latest 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 oauthtoken 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))
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 = new DBStore();
$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 = new FileStore("/Users/username/Documents/php_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:
namespace store;
use com\zoho\api\authenticator\Token;
use com\zoho\crm\api\exception\SDKException;
use com\zoho\crm\api\UserSignature;
use com\zoho\api\authenticator\store\TokenStore;
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.
*/
public function getToken($user, $token)
{
// 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.
*/
public function saveToken($user, $token)
{
// Add code to save the token
}
/**
* @param token A Token (com\zoho\api\authenticator\OAuthToken) class instance.
* @throws SDKException if any problem occurs.
*/
public function deleteToken($token)
{
// Add code to delete the token
}
/**
* @return array An array of Token (com\zoho\api\authenticator\OAuthToken) class instances
*/
public function getTokens()
{
//Add code to retrieve all the stored tokens
}
public function deleteTokens()
{
//Add code to delete all the stored tokens.
}
}