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.
- Login to your Zoho account.
- Visit https://api-console.zoho.com
- Click Self Client option of the client for which you wish to authorize.
- 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.
- 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.
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 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.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
* level -> Level of the log messages to be logged. Can be configured by typing Levels "." and choose any level from the list displayed.
* filePath -> Absolute file path, where messages need to be logged.
*/
val loggerInstance = new Logger.Builder()
.level(Logger.Levels.ALL)
..filePath("/Users/user_name/Documents/scala_sdk_log.log")
.build
//Create an UserSignature instance that takes user Email as parameter
val user = new UserSignature("abch.a@zoho.com")
/*
* Configure the environment
* which is of the pattern Domain.Environment
* Available Domains: USDataCenter, EUDataCenter, INDataCenter, CNDataCenter, AUDataCenter
* Available Environments: PRODUCTION, DEVELOPER, SANDBOX
*/
val env = USDataCenter.PRODUCTION
/*
* Create a Token instance that requires the following
* clientId -> OAuth client id.
* clientSecret -> OAuth client secret.
* refreshToken -> REFRESH token.
* grantToken -> GRANT token.
* id -> User unique id.
* redirectURL -> OAuth redirect URL.
*/
val token = new OAuthToken.Builder()
.clientID("clientId")
.clientSecret("clientSecret")
.refreshToken("refreshToken")
.redirectURL("redirectURL")
.build()
/*
* Create an instance of DBStore that requires the following
* host -> DataBase host name. Default value "localhost"
* databaseName -> DataBase name. Default value "zohooauth"
* userName -> DataBase user name. Default value "root"
* password -> DataBase password. Default value ""
* portNumber -> DataBase port number. Default value "3306"
* tabletName -> DataBase table name. Default value "oauthtoken"
*/
// val tokenstore = new DBStore.Builder().build
val tokenstore = new DBStore.Builder()
.host("hostName")
.databaseName("databaseName")
.tableName("tableName")
.userName("userName")
.password("password")
.portNumber("portNumber")
.build
// val 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
* A boolean field that validates user input for a pick list field and allows or disallows the addition of a new value to the list.
* if true - the SDK validates the input. If the value does not exist in the pick list, the SDK throws an error.
* if false - the SDK does not validate the input and makes the API request with the user’s input to the pick list
*/
var config : SDKConfig = new SDKConfig.Builder()
.pickListValidation(false)
.autoRefreshFields(false)
.build
val resourcePath = "/Users/user_name/Documents/scalasdk-application"
/*
* Create an instance of RequestProxy
* host -> proxyHost
* port -> proxyPort
* user -> proxyUser
* password -> password
* userDomain -> userDomain
*/
var requestProxy = new RequestProxy.Builder()
.host("proxyHost")
.port(80)
.user("proxyUser")
.password("password")
.userDomain("userDomain")
.build()
/*
* Set the following in InitializeBuilder
* user -> UserSignature instance
* environment -> Environment instance
* token -> Token instance
* store -> TokenStore instance
* SDKConfig -> SDKConfig instance
* resourcePath -> resourcePath - A String
* logger -> Log instance (optional)
* requestProxy -> RequestProxy instance (optional)
*/
new Initializer.Builder()
.user(user)
.environment(env)
.token(token)
.store(tokenstore)
.SDKConfig(config)
.resourcePath(resourcePath)
.logger(loggerInstance)
.initialize()
// token.remove()
}
}