Zig Requires Chilkat v11.0.0+
Zig
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 Zig Downloads
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;
};
// Replace values in all caps with your specific values.
// SHOP_CIPHER and SHOP_ID are returned from this example: Get Authorized Shops
const query_params = try chilkat.JsonObject.init();
defer query_params.deinit();
query_params.updateString("app_key", "APP_KEY") catch {};
query_params.updateString("shop_cipher", "SHOP_CIPHER") catch {};
query_params.updateString("shop_id", "SHOP_ID") 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 = "/product/202309/categories";
// 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/product/202309/categories", 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()});
var id: [:0]const u8 = "";
var is_leaf: bool = false;
var local_name: [:0]const u8 = "";
var parent_id: [:0]const u8 = "";
var j: i32 = 0;
var count_j: i32 = 0;
var str_val: [: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.categories");
while (i < count_i) {
json.setI(i);
id = try json.stringOf(alloc, "data.categories[i].id");
is_leaf = json.boolOf("data.categories[i].is_leaf");
local_name = try json.stringOf(alloc, "data.categories[i].local_name");
std.debug.print("local_name: {s}\n", .{local_name});
parent_id = try json.stringOf(alloc, "data.categories[i].parent_id");
j = 0;
count_j = json.sizeOfArray("data.categories[i].permission_statuses");
while (j < count_j) {
json.setJ(j);
str_val = try json.stringOf(alloc, "data.categories[i].permission_statuses[j]");
j = j + 1;
}
i = i + 1;
}
}