Initialization - C# SDK

Initialization - C# SDK

Now the app is ready to be initialized after defining configuration file/dictionary for your app.

Generating Grant tokens

For a Single User(self-authorized)

For self client apps, the self authorized grant token should be generated from the Zoho Developer Console ( https://accounts.zoho.com/developerconsole ). The developer console has an option to generate grant token for a user directly. This option may be handy when your app is going to use only one CRM user's credentials for all its operations or for your development testing.
  1. Login to the User's account.
  2. Visit  https://accounts.zoho.com/developerconsole
  3. Click on the  Options → Self Client  of the client for which you wish to authorize.
  4. Enter one or more (comma separated) valid  Zoho CRM scopes  that you wish to authorize in the “Scope” field and choose the time of expiry. Provide “ aaaserver.profile.READ ” scope along with Zoho CRM scopes.
  5. Copy the  grant token  that is displayed on the screen.
  6. Generate refresh_token from grant token by making a  POST  request with the URL below
  7. Copy the  refresh token  for backup.

Note

  • The generated grant token is valid only for the stipulated time you chose while generating it. Hence, the access and refresh tokens should be generated within that time.
  • The OAuth client registration and grant token generation must be done in the same Zoho account's (meaning - login) developer console.

For Multiple Users

For multiple users, it is the responsibility of your client app to generate the grant token from the users trying to login.

Your Application's UI must have a "Login with Zoho" option to open the grant token URL of Zoho, which would prompt for the user's Zoho login credentials.

Upon successful login of the user, the grant token will be sent as a param to your registered redirect URL.

Generating Access tokens

Access token can be generated by grant token or refresh token. Following any one of the two methods given below is sufficient.

From grant token

The following code snippet should be executed from your main class to get access token.
  1. "ZCRMRestClient.Initialize();
  2. ZohoOAuthClient client = ZohoOAuthClient.GetInstance();
  3. string grantToken = <paste_grant_token_here>;
  4. ZohoOAuthTokens tokens = client.GenerateAccessToken(grantToken);
  5. string accessToken = tokens.AccessToken;
  6. string refreshToken = tokens.RefreshToken;"

Please paste the generated grant token in the string literal mentioned. This is one time process only.

In case of multiple users using the application, you need to keep note of the following:

In order for the SDK to identify the particular user who made the request, the requester's email address should be given throught the following code snippet before making the actual method call of the SDK.
  1. ZCRMRestClient.SetCurrentUser("provide_current_user_email_here")
In case of Single users, the current user email can be set either through the above code, or in the zcrm_configuration section in the app.config file with the key currentUserEmail as a one time configuration.

From refresh token

The following code snippet should be executed from your main class to get access token.
  1. ZCRMRestClient.Initialize();
  2. ZohoOAuthClient client = ZohoOAuthClient.GetInstance();
  3. string refreshToken = <paste_refresh_token_here>;
  4. string userMailId = <provide_user_email_here>;
  5. ZohoOAuthTokens tokens = client. GenerateAccessTokenFromRefreshToken(refreshToken,userMailId);
Please paste the generated refresh token in the string literal mentioned. This is one time process only.


Note

  • The above code snippet is valid only once per grant token. Upon its successful execution, the generated access and refresh tokens would have been persisted through your persistence handler class.
  • Once the OAuth tokens have been persisted, subsequent API calls would use the persisted access and refresh tokens. The SDK will take care of refreshing the access token using refresh token, as and when required.

Start the App

The SDK requires the following line of code being invoked every time your app gets started.
  1. "ZCRMRestClient.Initialize();"

Note

This method should be called from the main class of your c# application to start the application. It needs to be invoked without any exception.

The SDK also allows for custom initialization, overriding the data from the app.config file. Or, you could also override when there is no need for the config file. The custom initialization scenarios are:
  1. "public static Dictionary<string, string> config = new Dictionary<string, string>()
  2. {
  3. {"client_id","1000.8ETLN5A9356890756HRWXWZ69VJCBN"},
  4. {"client_secret","b477d8bac9a8ad722334582b3430fdca7dde44de4e"},
  5. {"redirect_uri","<provide_redirect_url_here>"},
  6. {"access_type","offline"},
  7. {"persistence_handler_class","ZCRMSDK.OAuth.ClientApp.ZohoOAuthDBPersistence, ZCRMSDK"},
  8. {"oauth_tokens_file_path","<provide_file_path_here>"},
  9. {"mysql_username","root"},
  10. {"mysql_password",""},
  11. {"mysql_database","zohooauth"},
  12. {"mysql_server","localhost"},
  13. {"mysql_port","3306"},
  14. {"apiBaseUrl","https"// www.zohoapis.com "},
  15. {"photoUrl","<provide_photo_url_here>"},
  16. {"apiVersion","v2"},
  17. {"logFilePath","<provide_log_file_path_here>" },
  18. {"timeout",""},
  19. {"minLogLevel",""},
  20. {"domainSuffix","com"},
  21. {"currentUserEmail"," user@user.com "}
  22. };
  23. ZCRMRestClient.Initialize(config);"


Note

Once the SDK has been initialized, you can use any APIs of the SDK to get proper results.