Sample code for 30+ languages & platforms
Dart 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 Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

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

  final http = CkHttp();

  // 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
  final jsonToken = CkJsonObject();
  try {
    jsonToken.loadFile('qa_data/tokens/tiktok-shops.json');
  } on ChilkatException {
    print('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
  final queryParams = CkJsonObject();
  queryParams.updateString('app_key', 'APP_KEY');
  queryParams.updateString('shop_cipher', 'SHOP_CIPHER');
  queryParams.updateString('shop_id', 'SHOP_ID');
  final dt = 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..
  final ascending = true;
  final caseSensitive = true;
  queryParams.sort(ascending, caseSensitive);

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

  // Build the StringToSign
  final sb = CkStringBuilder();
  sb.append(appSecret);
  sb.append(path);
  final numParams = queryParams.size;
  var i = 0;
  while (i < numParams) {
    sb.append(queryParams.nameAt(i));
    sb.append(queryParams.stringAt(i));
    i++;
  }

  sb.append(appSecret);

  final crypt = CkCrypt2();
  crypt.hashAlgorithm = 'SHA256';
  crypt.macAlgorithm = 'HMAC';
  crypt.encodingMode = 'hex_lower';
  crypt.setMacKeyString(appSecret);

  final 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');

  final resp = CkHttpResponse();
  try {
    http.httpParams('GET', 'https://open-api.tiktokglobalshop.com/product/202309/categories', queryParams, resp);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final json = CkJsonObject();
  resp.getBodyJson(json);

  print(resp.statusCode);

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

  final code = json.intOf('code');
  final message = json.stringOf('message');
  final requestId = json.stringOf('request_id');
  i = 0;
  final 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');
    print('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++;
  }
}