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 IZohoPersistenceHandler interface, which has the following callback methods.
- SaveOAuthTokens(ZohoOAuthTokens tokens) — invoked while fetching:
- access and refresh tokens using grant token.
- access token using refresh token.
- DeleteOAuthTokens() — invoked before saving the newly received tokens.
- GetOAuthTokens()
— invoked before firing a request to fetch the saved tokens. This
method should return ZohoOAuthTokens object for the library to process
it.
Our C# SDK provides three sample implementations of IZohoPersistenceHandler interface within the client library. They are:
- ZohoOAuthFilePersistence
- ZohoOAuthDBPersistence
- ZohoOAuthInMemoryPersistence
The
name (along with its assembly comma seperated) of the implemented class
or the handlers provided by the SDK should be given as value for the
key peristence_handler_class.
Like ' persistence_handler_class=<persistence_handler_class, assembly_name> ', under the oauth_configuration section in the app.config file.
Note
- If the persistence handler class is not specified, InMemory Persistence handler handles the persistence implementation by default.
- Pre-defined persistence handler classes belong to the assembly ZCRMSDK.
ZohoOAuthFilePersistence
This method of persistence uses a local file to write and read the OAuth tokens.
The
complete path of the file to be used by the library to write and read
the tokens should be specified under the oauth_configuration section in
app.config file as the value of the key oauth_tokens_file_path .
ZohoOAuthDBPersistence
This method of persistence uses a custom MySQL persistence. To use this, you should make sure of the following.
- MySQL should be running in the same machine serving at the default port 3306.
- The database name should be zohooauth .
- There must be a table * oauthtokens * with the columns useridentifier (varchar(100)), accesstoken (varchar(100)), refreshtoken (varchar(100)) and expirytime (bigint).
ZohoOAuthInMemoryPersistence
Uses a singleton class to store and retrieve tokens. Default implementation and requires no external file.
Note
- ZohoOAuthFilePersistence
and ZohoOAuthInMemoryPersistence implementations only support to store
and refresh only a single user’s token. Hence they should be used only
if the app accesses Zoho APIs on behalf of a single user.
- In
case if the app has to support for multiple users, please use the
ZohoOAuthDBPersistence or write your own implementation of
IZohoPersistenceHandler.