Example

Invoke API

To generate an OAuth token and make a sample request, follow the steps mentioned below:

Steps to generate OAuth Token:

  1. Navigate to the OAuth section in this Documentation.
  2. Follow the first two steps to obtain your client_id, client_secret and code.
  3. Run the script provided below and provide the client_id, client_secret, and code obtained from the OAuth section.

The script will generate an access_token and refresh_token, make an API request, and provide the response.

Prerequisites to Run Scripts

Before running the script, ensure you have the following:

  • For python, install requests library using the command pip install requests.
  • For Java, Add the OkHttp library to your project.
    • For Maven, add the following dependency to your pom.xml.
    • <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>okhttp</artifactId>
        <version>4.9.0</version>
       </dependency>
    • For Gradle, add the following dependency to your build.gradle.
    • implementation 'com.squareup.okhttp3:okhttp:4.9.0'

Request Example

Click to copy
curl -X POST "https://accounts.zoho.com/oauth/v2/token" \ -H "Content-Type: application/x-www-form-urlencoded" \ -d "code=[your_code]&client_id=[your_client_id]&client_secret=[your_client_secret]&grant_type=authorization_code" curl -X GET "http://mdm.manageengine.ca/api/v1/mdm/devices -H "Authorization: Zoho-oauthtoken [your_access_token]"
import requests import json # Supported regions regions = { 1: "com", 2: "eu", 3: "in", 4: "com.au", 5: "jp", 6: "ca", 7: "cn", 8: "sa", 9: "uk" } # Get user inputs client_id = input("Enter Client ID: ") client_secret = input("Enter Client Secret: ") code = input("Enter Code: ") print("Select Region:") for key, value in regions.items(): print(f"{key} -> {value}") region_choice = int(input("Enter the number corresponding to your region: ")) region = regions.get(region_choice) # Request URL token_url = f"https://accounts.zoho.{region}/oauth/v2/token" params = { "code": code, "client_id": client_id, "client_secret": client_secret, "grant_type": "authorization_code" } try: # Post request to get tokens response = requests.post(token_url, data=params) response.raise_for_status() tokens = response.json() print("Token Response:", json.dumps(tokens, indent=4)) # Get access token access_token = tokens.get("access_token") # Get request to fetch devices devices_url = f"http://mdm.manageengine.{region}/api/v1/mdm/devices" headers = { "Authorization": f"Zoho-oauthtoken {access_token}" } devices_response = requests.get(devices_url, headers=headers) devices_response.raise_for_status() print("Devices Response:", json.dumps(devices_response.json(), indent=4)) except requests.exceptions.RequestException as e: print(f"An error occurred: {e}")
import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Scanner; import okhttp3.*; public class Script { public static void main(String[] args) throws IOException { // Supported regions Map regions = new HashMap<>(); regions.put(1, "com"); regions.put(2, "eu"); regions.put(3, "in"); regions.put(4, "com.au"); regions.put(5, "jp"); regions.put(6, "ca"); regions.put(7, "cn"); regions.put(8, "sa"); regions.put(9, "uk"); // Get user inputs Scanner scanner = new Scanner(System.in); System.out.print("Enter Client ID: "); String clientId = scanner.nextLine(); System.out.print("Enter Client Secret: "); String clientSecret = scanner.nextLine(); System.out.print("Enter Code: "); String code = scanner.nextLine(); System.out.println("Select Region:"); for (Map.Entry entry : regions.entrySet()) { System.out.println(entry.getKey() + " -> " + entry.getValue()); } System.out.print("Enter the number corresponding to your region: "); int regionChoice = scanner.nextInt(); String region = regions.get(regionChoice); // Request URL String tokenUrl = "https://accounts.zoho." + region + "/oauth/v2/token"; // Post request to get tokens OkHttpClient client = new OkHttpClient(); RequestBody body = new FormBody.Builder() .add("code", code) .add("client_id", clientId) .add("client_secret", clientSecret) .add("grant_type", "authorization_code") .build(); Request request = new Request.Builder() .url(tokenUrl) .post(body) .build(); Response response = client.newCall(request).execute(); String tokens = response.body().string(); System.out.println("Token Response: " + tokens); // Get access token String accessToken = tokens.split("\"access_token\":\"")[1].split("\"")[0]; // Get request to fetch devices String devicesUrl = "http://mdm.manageengine." + region + "/api/v1/mdm/devices"; Request devicesRequest = new Request.Builder() .url(devicesUrl) .addHeader("Authorization", "Zoho-oauthtoken " + accessToken) .build(); Response devicesResponse = client.newCall(devicesRequest).execute(); System.out.println("Devices Response: " + devicesResponse.body().string()); } }