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

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

  // 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/authorization/202309/shops', queryParams, resp);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

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

  print(resp.statusCode);
  print(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"
  // }

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

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