Sample code for 30+ languages & platforms
Perl

GetHarvest - Test Personal Access Token

See more GetHarvest Examples

Demonstrates how to test your GetHarvest Personal Access Token

Chilkat Perl Downloads

Perl
use strict;
use warnings;
use Chilkat2;

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

my $http = Chilkat2::Http->new;

$http->SetRequestHeader('User-Agent', 'Harvest API Example');
$http->SetRequestHeader('Harvest-Account-ID', 'ACCOUNT_ID');
$http->SetRequestHeader('Authorization', 'Bearer ACCESS_TOKEN');

my $sbResponseBody = Chilkat2::StringBuilder->new;
my $success = $http->QuickGetSb('https://api.harvestapp.com/api/v2/users/me.json', $sbResponseBody);
if (!$success) {
    print $http->LastErrorText, "\n";
    exit;
}

my $jResp = Chilkat2::JsonObject->new;
$jResp->LoadSb($sbResponseBody);
$jResp->EmitCompact(0);

print "Response Body:\n";
print $jResp->Emit(), "\n";

my $respStatusCode = $http->LastStatus;
print 'Response Status Code = ', $respStatusCode, "\n";

if ($respStatusCode >= 400) {
    print "Response Header:\n";
    print $http->LastResponseHeader, "\n";
    exit;
}

# 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

my $strVal = '';

my $id = $jResp->IntOf('id');
my $first_name = $jResp->StringOf('first_name');
my $last_name = $jResp->StringOf('last_name');
my $email = $jResp->StringOf('email');
my $telephone = $jResp->StringOf('telephone');
my $timezone = $jResp->StringOf('timezone');
my $weekly_capacity = $jResp->IntOf('weekly_capacity');
my $has_access_to_all_future_projects = $jResp->BoolOf('has_access_to_all_future_projects');
my $is_contractor = $jResp->BoolOf('is_contractor');
my $is_admin = $jResp->BoolOf('is_admin');
my $is_project_manager = $jResp->BoolOf('is_project_manager');
my $can_see_rates = $jResp->BoolOf('can_see_rates');
my $can_create_projects = $jResp->BoolOf('can_create_projects');
my $can_create_invoices = $jResp->BoolOf('can_create_invoices');
my $is_active = $jResp->BoolOf('is_active');
my $created_at = $jResp->StringOf('created_at');
my $updated_at = $jResp->StringOf('updated_at');
my $default_hourly_rate = $jResp->StringOf('default_hourly_rate');
my $cost_rate = $jResp->StringOf('cost_rate');
my $avatar_url = $jResp->StringOf('avatar_url');
my $i = 0;
my $count_i = $jResp->SizeOfArray('roles');
while ($i < $count_i) {
    $jResp->I($i);
    $strVal = $jResp->StringOf('roles[i]');
    $i = $i + 1;
}