Sample code for 30+ languages & platforms
React Native

RSAP Union API - Get Members Status

See more _Miscellaneous_ Examples

Demonstrates how to use an OAuth2 access token for the RSAP Union API. Calls the endpoint to get the statuses of all union members.

Chilkat React Native Downloads

React Native
import { Cert, Http, JsonObject, PrivateKey, 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();

  // Load the access token previously obtained by this example:  RSAP Union OAuth2
  const jToken = new JsonObject();
  try {
    jToken.loadFile('qa_data/tokens/rsapToken.json');
  } catch {
    console.log('Failed to load access token JSON.');
    return;
  }

  // Adds the "Authorization: Bearer ACCESS_TOKEN" header.
  http.authToken = jToken.stringOf('access_token');

  // For authentication, assuming both the client cert and access token are needed???
  const cert = new Cert();
  try {
    cert.loadFromFile('qa_data/certs_and_keys/union_client_certificate.crt');
  } catch {
    console.log(cert.lastErrorText);
    return;
  }

  const privKey = new PrivateKey();
  try {
    privKey.loadAnyFormatFile('qa_data/certs_and_keys/union_client_certificate.nopass.key', '');
  } catch {
    console.log(privKey.lastErrorText);
    return;
  }

  // Associate the private key with the cert.
  // This will fail if the private key is not actually the correct one that corresponds to the public key stored within the cert.
  try {
    cert.setPrivateKey(privKey);
  } catch {
    console.log(cert.lastErrorText);
    return;
  }

  // Tell HTTP to use the cert for client TLS certificate authentication.
  try {
    http.setSslClientCert(cert);
  } catch {
    console.log(http.lastErrorText);
    return;
  }

  const sbResponseBody = new StringBuilder();
  try {
    await http.quickGetSbAsync('https://api-test.rsap.ca/members/status', 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.lastHeader);
    console.log('Failed.');
    return;
  }
}