Initialization - Java SDK

Initialization - Java SDK

Now the app is ready to be initialized after defining OAuth configuration file and OAuth persistence handler class for your app.

Generating grant tokens

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 the User's account.
  2. Visit https://accounts.zoho.com/developerconsole
  3. Click on the Options → 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.

Generating access and refresh tokens

After obtaining the grant token, the following code snippet should be put in a java file and executed from your main class to get access and refresh tokens. 

  1. ZCRMRestClient.initialize();
  2. ZohoOAuthClient cli = ZohoOAuthClient.getInstance();
  3. String grantToken = “paste_the_self_authorized_grant_token_here”;
  4. ZohoOAuthTokens tokens = cli.generateAccessToken(grantToken);
  5. String accessToken = tokens.getAccessToken();
  6. String refreshToken = tokens.getRefreshToken();
  7. System.out.println("access token = " + accessToken + " & refresh token = " + refreshToken);

Access and refresh tokens are generated and stored in the oauth_tokens.properties file if you are using ZohoOAuthFilePersistence and the same will be stored in your Mysql database table "oauthtokens" if you are using ZohoOAuthDBPersistence, on executing the above java code.

Note
  1. 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.
  2. 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.



    • Related Articles

    • Java SDK - Overview

      Java SDK offers a way to create client java applications that can be integrated with Zoho CRM. This SDK makes the access and use of necessary CRM APIs with ease. In other words, it serves as a wrapper for the REST APIs, making it easier to use the ...
    • Release Notes - Java SDK

      ZCRMSDK -VERSION 1.0.6  Downloadable JAR file  You can download the jar file for the version1.0.6 of Java SDKs at the end of this page.  Install command - Maven:(in pom.xml)  <repositories>        <repository>              ...
    • Responses & Exceptions - Java SDK

      APIResponse, BulkAPIResponse and FileAPIResponse are the wrapper objects for Zoho CRM APIs’ responses. All API calling methods would return one of these three objects. A method-seeking entity would return APIResponse object, whereas a method-seeking ...
    • Persistence - Java SDK

      Persistent classes in an application implement the entities of the business problem. In Java SDK, two default persistence classes with their implementations are provided. On the other hand, if a developer wants his specific implementation, he can ...
    • Configuration - Java SDK

      Setting up the configuration is a system engineering process, for establishing and maintaining the consistency of the application's performance, functional and physical attributes with its requirements, design and operational information throughout ...