Initializing the Application

To access the CRM services through the SDK, you must first authenticate your client app.

Generating the grant token

For a Single User

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 your Zoho account.
  2. Visit https://api-console.zoho.com
  3. Click Self Client option 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.
  5. Copy the grant token that is displayed on the screen.

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.
Note
  • The access and refresh tokens are environment-specific and domain-specific. When you handle various environments and domains such as Production, Sandbox, or Developer and IN, CN, US, EU, or AU, respectively, you must use the access token and refresh token generated only in those respective environments and domains. The SDK throws an error, otherwise.
    For example, if you generate the tokens for your Sandbox environment in the CN domain, you must use only those tokens for that domain and environment. You cannot use the tokens generated for a different environment or a domain.

  • Initializing the SDK does not generate an access token. An access token is generated only when you make an API call.

Initialization


import com.zoho.api.authenticator.OAuthToken
import com.zoho.api.authenticator.Token
import com.zoho.api.authenticator.OAuthToken.TokenType
import com.zoho.api.authenticator.store.DBStore
import com.zoho.api.authenticator.store.FileStore
import com.zoho.api.authenticator.store.TokenStore
import com.zoho.crm.api
import com.zoho.crm.api.{Initializer, RequestProxy, SDKConfig, UserSignature}
import com.zoho.crm.api.dc.DataCenter.Environment
import com.zoho.crm.api.dc.USDataCenter
import com.zoho.api.logger.Logger
import com.zoho.api.logger.Logger.Levels


object Initialize {
      @throws[Exception]
      def main(args: Array[String]): Unit = {
        initialize()
      }

      @throws[Exception]
      def initialize(): Unit = {
        /*
        * Create an instance of Logger Class that takes two parameters
        * 1 -> Level of the log messages to be logged. Can be configured by typing Levels "." and choose any level from the list displayed.
        * 2 -> Absolute file path, where messages need to be logged.
        */
        var logger = Logger.getInstance(Levels.INFO, "/Users/user_name/Documents/scala_sdk_log.log")

        //Create an UserSignature instance that takes user Email as parameter
        var user = new UserSignature("abc@zoho.com")

        /*
        * Configure the environment
        * which is of the pattern Domain.Environment
        * Available Domains: USDataCenter, EUDataCenter, INDataCenter, CNDataCenter, AUDataCenter
        * Available Environments: PRODUCTION, DEVELOPER, SANDBOX
        */
        var environment = USDataCenter.PRODUCTION

        /*
        * Create a Token instance
        * 1 -> OAuth client id.
        * 2 -> OAuth client secret.
        * 3 -> REFRESH/GRANT token.
        * 4 -> Token type(REFRESH/GRANT).
        * 5 -> OAuth redirect URL.
        */
        var token = new OAuthToken("clientId", "clientSecret", "REFRESH/GRANT token", TokenType.REFRESH/GRANT, Option("redirectURL"))

        /*
        * Create an instance of TokenStore.
        * 1 -> DataBase host name. Default "localhost"
        * 2 -> DataBase name. Default "zohooauth"
        * 3 -> DataBase user name. Default "root"
        * 4 -> DataBase password. Default ""
        * 5 -> DataBase port number. Default "3306"
        */
        //TokenStore tokenstore = new DBStore()
        var tokenstore = new DBStore(Option("hostName"), Option("dataBaseName"), Option("userName"), Option("password"), Option("portNumber"))

        //var tokenstore = new FileStore("absolute_file_path")

        /*
         * autoRefreshFields
         * if true - all the modules' fields will be auto-refreshed in the background, every    hour.
         * if false - the fields will not be auto-refreshed in the background. The user can manually delete the file(s) or refresh the fields using methods from ModuleFieldsHandler(com.zoho.crm.api.util.ModuleFieldsHandler)
         *
         * pickListValidation
         * if true - value for any picklist field will be validated with the available values.
         * if false - value for any picklist field will not be validated, resulting in creation of a new value.
          * 
          * connectionTimeout
          * A Integer field to set connection timeout 
          * 
          * requestTimeout
          * A Integer field to set request timeout 
          * 
          * socketTimeout
          * A Integer field to set socket timeout 
         */
        var sdkConfig = new SDKConfig.Builder().setAutoRefreshFields(false).setPickListValidation(true).connectionTimeout(1000).requestTimeout(1000).socketTimeout(1000).build()

        var resourcePath = "/Users/user_name/Documents/scalasdk-application"

        /**
         * Create an instance of RequestProxy class that takes the following parameters
         * 1 -> Host
         * 2 -> Port Number
         * 3 -> User Name
         * 4 -> Password
         * 5 -> User Domain
         */
        var requestProxy = new RequestProxy("proxyHost", "proxyPort", Option ("proxyUser"), Option ("password"), Option ("userDomain"))

        /*
        * The initialize method of Initializer class that takes the following arguments
        * 1 -> UserSignature instance
        * 2 -> Environment instance
        * 3 -> Token instance
        * 4 -> TokenStore instance
        * 5 -> SDKConfig instance
        * 6 -> resourcePath -A String
        * 7 -> Logger instance
        * 8 -> RequestProxy instance
        */

        // The following are the available initialize methods

        Initializer.initialize(user, environment, token, tokenstore, sdkConfig, resourcePath, Option(logger), Option(requestProxy))
    }