Sample Codes
Below are a few sample API methods available in the Zoho Desk Java SDK. You can get all module instance from ZDesk class.
SDK initialization and access token generation process
These examples help you to initializate and generate access token from the SDK.
Initialize SDK and Generating access token from self client grant token
Copy to clipboardimport java.io.File; import com.zoho.oauth.client.ZohoOAuthClient; import com.zoho.oauth.common.ZohoOAuthException; import com.zoho.oauth.contract.ZohoOAuthTokens; public class App { public static void main(String[] args) { try { //We assume that you set the path of the configuration property file in the system property //The SDK automatically initialized when you try to create a ZohoOAuthClient instance. ZohoOAuthClient client = ZohoOAuthClient.getInstance(); //If no error occurs while generating the access token, oauth token data is generated and stored in the persistence class specified. ZohoOAuthTokens tokens = client.generateAccessToken("1000.cb07c247e14dd6005b94a03b141897c2.2e1d87264a743a2f5ec42acc357fe73d"); //self_client_grant_token is passed as arg of the method System.out.println(tokens.getAccessToken() + " ::: " + tokens.getRefreshToken()); } catch(ZohoOAuthException ex) { ex.printStackTrace(); } } }
Initialize SDK and Generating access token from your Web App
Copy to clipboard//We assume that the API call to initialize the app comes to this method. //We also assume that you set the path of the configuration property file in the system property. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //When you call - ZohoOAuthClient.getInstance().getLoginWithZohoUrl(); the SDK starts to get initialized. //Then, a valid Zoho account URL is returned, based on your configuration property data. response.sendRedirect(ZohoOAuthClient.getInstance().getLoginWithZohoUrl()) } //After the app permissions are accepted, Zoho calls the redirectURI specified in the property file. //We assume that the redirect API call comes to this method. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String code = request.getParameter("code"); //getting grant_token from request ZohoOAuthClient client = ZohoOAuthClient.getInstance(); try { //If no error occurs while generating the access token, oauth token data is generated and stored in the persistence class specified. client.generateAccessToken(code); } catch (ZohoOAuthException e) { e.printStackTrace(); } }
Ticket Operations
These methods help you create, read, update, and delete tickets in your Zoho Desk portal.
createTicket This method creates a ticket
Copy to clipboardimport java.io.File; import com.zoho.desk.exception.ZDeskException; import com.zoho.desk.init.ZDesk; import com.zoho.desk.ticket.Ticket; import com.zoho.desk.ticket.TicketAPI; import com.zoho.oauth.client.ZohoOAuthClient; import com.zoho.oauth.common.ZohoOAuthException; public class App { public static void main(String[] args) { try { //We assume that you set the path of the configuration property file in the system property TicketAPI ticketAPI = ZDesk.getTicketAPIInstance("steve.sp@zylker.com"); //mailId is passed as arg of the method. Ticket ticket = new Ticket(); ticket.setSubject("Ticket From Java Client"); ticket.setDepartmentId("291783000000006907"); ticket.setContactId("291783000000101077"); ticket.setStatus("Open"); Ticket response = ticketAPI.createTicket(ticket); System.out.println(response); } catch(ZDeskException | ZohoOAuthException ex) { ex.printStackTrace(); } } }
updateTicket This method helps update the details of a ticket
Copy to clipboardimport java.io.File; import com.zoho.desk.exception.ZDeskException; import com.zoho.desk.init.ZDesk; import com.zoho.desk.ticket.Ticket; import com.zoho.desk.ticket.TicketAPI; import com.zoho.oauth.client.ZohoOAuthClient; import com.zoho.oauth.common.ZohoOAuthException; public class App { public static void main(String[] args) { try { //We assume that you set the path of the configuration property file in the system property TicketAPI ticketAPI = ZDesk.getTicketAPIInstance("steve.sp@zylker.com"); //mailId is passed as arg of the method. Ticket ticket = new Ticket(); ticket.setDescription("Ticket Description added from Java client"); Ticket response = ticketAPI.updateTicket("291783000000906001", ticket); System.out.println(response); } catch(ZDeskException | ZohoOAuthException ex) { ex.printStackTrace(); } } }
moveToTrash This method deletes tickets
Copy to clipboardimport java.io.File; import java.util.ArrayList; import java.util.List; import com.zoho.desk.exception.ZDeskException; import com.zoho.desk.init.ZDesk; import com.zoho.desk.ticket.TicketAPI; import com.zoho.oauth.client.ZohoOAuthClient; import com.zoho.oauth.common.ZohoOAuthException; public class App { public static void main(String[] args) { try { //We assume that you set the path of the configuration property file in the system property TicketAPI ticketAPI = ZDesk.getTicketAPIInstance("steve.sp@zylker.com"); //mailId is passed as arg of the method. List
ticketIds = new ArrayList(); ticketIds.add("291783000000906001"); boolean response = ticketAPI.moveToTrash(ticketIds); System.out.println("Is successful: " + response); } catch(ZDeskException | ZohoOAuthException ex) { ex.printStackTrace(); } } }
getTicket This method fetches the details of a single ticket
Copy to clipboardimport java.io.File; import com.zoho.desk.exception.ZDeskException; import com.zoho.desk.init.ZDesk; import com.zoho.desk.ticket.Ticket; import com.zoho.desk.ticket.TicketAPI; import com.zoho.oauth.client.ZohoOAuthClient; import com.zoho.oauth.common.ZohoOAuthException; public class App { public static void main(String[] args) { try { //We assume that you set the path of the configuration property file in the system property TicketAPI ticketAPI = ZDesk.getTicketAPIInstance("steve.sp@zylker.com"); //mailId is passed as arg of the method. Ticket response = ticketAPI.getTicket("291783000000906001", null); System.out.println(response); } catch(ZDeskException | ZohoOAuthException ex) { ex.printStackTrace(); } } }