Sample code for 30+ languages & platforms
Lua

GetHarvest - Test Personal Access Token

See more GetHarvest Examples

Demonstrates how to test your GetHarvest Personal Access Token

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.

local http = chilkat.Http.new()

http:set_request_header("User-Agent", "Harvest API Example")
http:set_request_header("Harvest-Account-ID", "ACCOUNT_ID")
http:set_request_header("Authorization", "Bearer ACCESS_TOKEN")

local sbResponseBody = chilkat.StringBuilder.new()
local success = http:quick_get_sb("https://api.harvestapp.com/api/v2/users/me.json", sbResponseBody)
if not success then
    print(http.last_error_text)
    return
end

local jResp = chilkat.JsonObject.new()
jResp:load_sb(sbResponseBody)
jResp.emit_compact = false

print("Response Body:")
print(jResp:emit())

local respStatusCode = http.last_status
print("Response Status Code = " .. respStatusCode)

if respStatusCode >= 400 then
    print("Response Header:")
    print(http.last_response_header)
    return
end

-- 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

local strVal = ""

local id = jResp:int_of("id")
local first_name = jResp:string_of("first_name")
local last_name = jResp:string_of("last_name")
local email = jResp:string_of("email")
local telephone = jResp:string_of("telephone")
local timezone = jResp:string_of("timezone")
local weekly_capacity = jResp:int_of("weekly_capacity")
local has_access_to_all_future_projects = jResp:bool_of("has_access_to_all_future_projects")
local is_contractor = jResp:bool_of("is_contractor")
local is_admin = jResp:bool_of("is_admin")
local is_project_manager = jResp:bool_of("is_project_manager")
local can_see_rates = jResp:bool_of("can_see_rates")
local can_create_projects = jResp:bool_of("can_create_projects")
local can_create_invoices = jResp:bool_of("can_create_invoices")
local is_active = jResp:bool_of("is_active")
local created_at = jResp:string_of("created_at")
local updated_at = jResp:string_of("updated_at")
local default_hourly_rate = jResp:string_of("default_hourly_rate")
local cost_rate = jResp:string_of("cost_rate")
local avatar_url = jResp:string_of("avatar_url")
local i = 0
local count_i = jResp:size_of_array("roles")
while i < count_i do
    jResp.i = i
    strVal = jResp:string_of("roles[i]")
    i = i + 1
end