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

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

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

    const http = try chilkat.Http.init();
    defer http.deinit();

    // 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 json_token = try chilkat.JsonObject.init();
    defer json_token.deinit();
    json_token.loadFile("qa_data/tokens/tiktok-shops.json") catch {
        std.debug.print("Failed to load tiktok-shops.json\n", .{});
        return;
    };

    const query_params = try chilkat.JsonObject.init();
    defer query_params.deinit();
    query_params.updateString("app_key", "APP_KEY") catch {};
    const dt = try chilkat.DateTime.init();
    defer dt.deinit();
    query_params.updateString("timestamp", try dt.getAsUnixTimeStr(alloc, false)) catch {};
    query_params.updateInt("version", 202309) catch {};

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

    const app_secret = "APP_SECRET";
    const path = "/authorization/202309/shops";

    // Build the StringToSign
    const sb = try chilkat.StringBuilder.init();
    defer sb.deinit();
    sb.append(app_secret) catch {};
    sb.append(path) catch {};
    const num_params = query_params.getSize();
    var i: i32 = 0;
    while (i < num_params) {
        sb.append(try query_params.nameAt(alloc, i)) catch {};
        sb.append(try query_params.stringAt(alloc, i)) catch {};
        i = i + 1;
    }

    sb.append(app_secret) catch {};

    const crypt = try chilkat.Crypt2.init();
    defer crypt.deinit();
    crypt.setHashAlgorithm("SHA256");
    crypt.setMacAlgorithm("HMAC");
    crypt.setEncodingMode("hex_lower");
    crypt.setMacKeyString(app_secret) catch {};

    const sig = try crypt.macStringENC(alloc, try sb.getAsString(alloc));

    query_params.updateString("access_token", try json_token.stringOf(alloc, "data.access_token")) catch {};
    query_params.updateString("sign", sig) catch {};

    http.setRequestHeader("x-tts-access-token", try json_token.stringOf(alloc, "data.access_token"));
    http.setRequestHeader("content-type", "application/json");

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpParams("GET", "https://open-api.tiktokglobalshop.com/authorization/202309/shops", query_params, resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    resp.getBodyJson(json) catch {};

    std.debug.print("{d}\n", .{resp.getStatusCode()});
    std.debug.print("{s}\n", .{try resp.getBodyStr(alloc)});

    // 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: [:0]const u8 = "";
    var code_str: [:0]const u8 = "";
    var id: [:0]const u8 = "";
    var name: [:0]const u8 = "";
    var region: [:0]const u8 = "";
    var seller_type: [:0]const u8 = "";

    _ = json.intOf("code");
    _ = try json.stringOf(alloc, "message");
    _ = try json.stringOf(alloc, "request_id");
    i = 0;
    const count_i = json.sizeOfArray("data.shops");
    while (i < count_i) {
        json.setI(i);
        cipher = try json.stringOf(alloc, "data.shops[i].cipher");
        code_str = try json.stringOf(alloc, "data.shops[i].code");
        id = try json.stringOf(alloc, "data.shops[i].id");
        name = try json.stringOf(alloc, "data.shops[i].name");
        region = try json.stringOf(alloc, "data.shops[i].region");
        seller_type = try json.stringOf(alloc, "data.shops[i].seller_type");
        i = i + 1;
    }
}