Sample code for 30+ languages & platforms
React Native

GetHarvest - Test Personal Access Token

See more GetHarvest Examples

Demonstrates how to test your GetHarvest Personal Access Token

Chilkat React Native Downloads

React Native
import { Http, JsonObject, StringBuilder } from '@chilkat/react-native'

async function chilkatExample() {
  // This example assumes the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  const http = new Http();

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

  const sbResponseBody = new StringBuilder();
  try {
    await http.quickGetSbAsync('https://api.harvestapp.com/api/v2/users/me.json', sbResponseBody);
  } catch {
    console.log(http.lastErrorText);
    return;
  }

  const jResp = new JsonObject();
  jResp.loadSb(sbResponseBody);
  jResp.emitCompact = false;

  console.log('Response Body:');
  console.log(jResp.emit());

  const respStatusCode = http.lastStatus;
  console.log(`Response Status Code = ${respStatusCode}`);

  if (respStatusCode >= 400) {
    console.log('Response Header:');
    console.log(http.lastResponseHeader);
    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

  let i = 0;
  let strVal = '';

  const id = jResp.intOf('id');
  const firstName = jResp.stringOf('first_name');
  const lastName = jResp.stringOf('last_name');
  const email = jResp.stringOf('email');
  const telephone = jResp.stringOf('telephone');
  const timezone = jResp.stringOf('timezone');
  const weeklyCapacity = jResp.intOf('weekly_capacity');
  const createdAt = jResp.stringOf('created_at');
  const updatedAt = jResp.stringOf('updated_at');
  const defaultHourlyRate = jResp.stringOf('default_hourly_rate');
  const costRate = jResp.stringOf('cost_rate');
  const avatarUrl = jResp.stringOf('avatar_url');
  i = 0;
  const countI = jResp.sizeOfArray('roles');
  while (i < countI) {
    jResp.i = i;
    strVal = jResp.stringOf('roles[i]');
    i++;
  }
}