Sample code for 30+ languages & platforms
Java

GetHarvest - Test Personal Access Token

See more GetHarvest Examples

Demonstrates how to test your GetHarvest Personal Access Token

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example assumes the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Http http = new Http();

        http.setRequestHeader("User-Agent", "Harvest API Example");
        http.setRequestHeader("Harvest-Account-ID", "ACCOUNT_ID");
        http.setRequestHeader("Authorization", "Bearer ACCESS_TOKEN");

        StringBuilder sbResponseBody = new StringBuilder();
        try {
            http.quickGetSb("https://api.harvestapp.com/api/v2/users/me.json", sbResponseBody);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        JsonObject jResp = new JsonObject();
        jResp.loadSb(sbResponseBody);
        jResp.setEmitCompact(false);

        System.out.println("Response Body:");
        System.out.println(jResp.emit());

        int respStatusCode = http.getLastStatus();
        System.out.println("Response Status Code = " + respStatusCode);

        if (respStatusCode >= 400) {
            System.out.println("Response Header:");
            System.out.println(http.getLastResponseHeader());
            return;
        }

        // Sample JSON response:

        // {
        //   "id": 2826142,
        //   "first_name": "Joe",
        //   "last_name": "Johnson",
        //   "email": "joe@example.com",
        //   "telephone": "",
        //   "timezone": "Central Time (US & Canada)",
        //   "weekly_capacity": 126000,
        //   "has_access_to_all_future_projects": false,
        //   "is_contractor": false,
        //   "is_admin": true,
        //   "is_project_manager": false,
        //   "can_see_rates": true,
        //   "can_create_projects": true,
        //   "can_create_invoices": true,
        //   "is_active": true,
        //   "created_at": "2019-06-06T15:57:58Z",
        //   "updated_at": "2019-06-06T15:58:32Z",
        //   "default_hourly_rate": 175.0,
        //   "cost_rate": 60.0,
        //   "roles": [
        //     "role1",
        //     "role2"
        //   ],
        //   "avatar_url": "https://d3s3969qhosaug.cloudfront.net/default-avatars/4d46.png?1559836678"
        // }

        // Sample code for parsing the JSON response...
        // Use the following online tool to generate parsing code from sample JSON:
        // Generate Parsing Code from JSON

        String strVal = "";

        int id = jResp.intOf("id");
        String first_name = jResp.stringOf("first_name");
        String last_name = jResp.stringOf("last_name");
        String email = jResp.stringOf("email");
        String telephone = jResp.stringOf("telephone");
        String timezone = jResp.stringOf("timezone");
        int weekly_capacity = jResp.intOf("weekly_capacity");
        boolean has_access_to_all_future_projects = jResp.boolOf("has_access_to_all_future_projects");
        boolean is_contractor = jResp.boolOf("is_contractor");
        boolean is_admin = jResp.boolOf("is_admin");
        boolean is_project_manager = jResp.boolOf("is_project_manager");
        boolean can_see_rates = jResp.boolOf("can_see_rates");
        boolean can_create_projects = jResp.boolOf("can_create_projects");
        boolean can_create_invoices = jResp.boolOf("can_create_invoices");
        boolean is_active = jResp.boolOf("is_active");
        String created_at = jResp.stringOf("created_at");
        String updated_at = jResp.stringOf("updated_at");
        String default_hourly_rate = jResp.stringOf("default_hourly_rate");
        String cost_rate = jResp.stringOf("cost_rate");
        String avatar_url = jResp.stringOf("avatar_url");
        int i = 0;
        int count_i = jResp.sizeOfArray("roles");
        while (i < count_i) {
            jResp.setI(i);
            strVal = jResp.stringOf("roles[i]");
            i = i + 1;
        }
    }
}