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

TikTok Shop Get Authorized Shops

See more TikTok Shop Examples

An example showing how to get the authorized shops in the TikTok Shops API.

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;
  }

  const queryParams = new JsonObject();
  queryParams.updateString('app_key', 'APP_KEY');
  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 = '/authorization/202309/shops';

  // 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/authorization/202309/shops', queryParams, resp);
  } catch {
    console.log(http.lastErrorText);
    return;
  }

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

  console.log(resp.statusCode);
  console.log(resp.bodyStr);

  // Sample JSON response...
  // Use this online tool to generate parsing code from sample JSON: 
  // Generate Parsing Code from JSON

  // {
  //   "code": 0,
  //   "data": {
  //     "shops": [
  //       {
  //         "cipher": "GCP_XF90igAAAABh00qsWgtvOiGFNqyubMt3",
  //         "code": "CNGBCBA4LLU8",
  //         "id": "7000714532876273420",
  //         "name": "Maomao beauty shop",
  //         "region": "GB",
  //         "seller_type": "CROSS_BORDER"
  //       }
  //     ]
  //   },
  //   "message": "Success",
  //   "request_id": "202203070749000101890810281E8C70B7"
  // }

  let cipher = '';
  let codeStr = '';
  let id = '';
  let name = '';
  let region = '';
  let sellerType = '';

  const code = json.intOf('code');
  const message = json.stringOf('message');
  const requestId = json.stringOf('request_id');
  i = 0;
  const countI = json.sizeOfArray('data.shops');
  while (i < countI) {
    json.i = i;
    cipher = json.stringOf('data.shops[i].cipher');
    codeStr = json.stringOf('data.shops[i].code');
    id = json.stringOf('data.shops[i].id');
    name = json.stringOf('data.shops[i].name');
    region = json.stringOf('data.shops[i].region');
    sellerType = json.stringOf('data.shops[i].seller_type');
    i++;
  }
}