Sample code for 30+ languages & platforms
React Native Requires Chilkat v11.0.0+

TikTok Shop Get Categories

See more TikTok Shop Examples

An example showing how to use a TikTok Shops access token in an API call.

Chilkat React Native Downloads

React Native
import { CkDateTime, Crypt2, Http, HttpResponse, 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();

  // It is assumed we previously obtained an OAuth2 access token.
  // This example loads the JSON access token file 
  // saved by this example: Get TikTok Shop OAuth2 Access Token
  // or refrehsed by this example: Get TikTok Shop Refresh OAuth2 Access Token
  const jsonToken = new JsonObject();
  try {
    jsonToken.loadFile('qa_data/tokens/tiktok-shops.json');
  } catch {
    console.log('Failed to load tiktok-shops.json');
    return;
  }

  // Replace values in all caps with your specific values.
  // SHOP_CIPHER and SHOP_ID are returned from this example: Get Authorized Shops
  const queryParams = new JsonObject();
  queryParams.updateString('app_key', 'APP_KEY');
  queryParams.updateString('shop_cipher', 'SHOP_CIPHER');
  queryParams.updateString('shop_id', 'SHOP_ID');
  const dt = new CkDateTime();
  queryParams.updateString('timestamp', dt.getAsUnixTimeStr(false));
  queryParams.updateInt('version', 202309);

  // Sort the JSON members by member name, in ascending order (A-Z), case sensitive..
  const ascending = true;
  const caseSensitive = true;
  queryParams.sort(ascending, caseSensitive);

  const appSecret = 'APP_SECRET';
  const path = '/product/202309/categories';

  // Build the StringToSign
  const sb = new StringBuilder();
  sb.append(appSecret);
  sb.append(path);
  const numParams = queryParams.size;
  let i = 0;
  while (i < numParams) {
    sb.append(queryParams.nameAt(i));
    sb.append(queryParams.stringAt(i));
    i++;
  }

  sb.append(appSecret);

  const crypt = new Crypt2();
  crypt.hashAlgorithm = 'SHA256';
  crypt.macAlgorithm = 'HMAC';
  crypt.encodingMode = 'hex_lower';
  crypt.setMacKeyString(appSecret);

  const sig = crypt.macStringENC(sb.getAsString());

  queryParams.updateString('access_token', jsonToken.stringOf('data.access_token'));
  queryParams.updateString('sign', sig);

  http.setRequestHeader('x-tts-access-token', jsonToken.stringOf('data.access_token'));
  http.setRequestHeader('content-type', 'application/json');

  const resp = new HttpResponse();
  try {
    await http.httpParamsAsync('GET', 'https://open-api.tiktokglobalshop.com/product/202309/categories', queryParams, resp);
  } catch {
    console.log(http.lastErrorText);
    return;
  }

  const json = new JsonObject();
  resp.getBodyJson(json);

  console.log(resp.statusCode);

  let id = '';
  let localName = '';
  let parentId = '';
  let j = 0;
  let countJ = 0;
  let strVal = '';

  const code = json.intOf('code');
  const message = json.stringOf('message');
  const requestId = json.stringOf('request_id');
  i = 0;
  const countI = json.sizeOfArray('data.categories');
  while (i < countI) {
    json.i = i;
    id = json.stringOf('data.categories[i].id');
    json.boolOf('data.categories[i].is_leaf');
    localName = json.stringOf('data.categories[i].local_name');
    console.log(`local_name: ${localName}`);
    parentId = json.stringOf('data.categories[i].parent_id');
    j = 0;
    countJ = json.sizeOfArray('data.categories[i].permission_statuses');
    while (j < countJ) {
      json.j = j;
      strVal = json.stringOf('data.categories[i].permission_statuses[j]');
      j++;
    }

    i++;
  }
}