Lua Requires Chilkat v11.0.0+
Lua
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 Lua Downloads
local chilkat = require("chilkat")
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local http = chilkat.Http.new()
-- 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
local jsonToken = chilkat.JsonObject.new()
local success = jsonToken:load_file("qa_data/tokens/tiktok-shops.json")
if not success then
print("Failed to load tiktok-shops.json")
return
end
-- Replace values in all caps with your specific values.
-- SHOP_CIPHER and SHOP_ID are returned from this example: Get Authorized Shops
local queryParams = chilkat.JsonObject.new()
queryParams:update_string("app_key", "APP_KEY")
queryParams:update_string("shop_cipher", "SHOP_CIPHER")
queryParams:update_string("shop_id", "SHOP_ID")
local dt = chilkat.DateTime.new()
queryParams:update_string("timestamp", dt:get_as_unix_time_str(false))
queryParams:update_int("version", 202309)
-- Sort the JSON members by member name, in ascending order (A-Z), case sensitive..
local ascending = true
local caseSensitive = true
queryParams:sort(ascending, caseSensitive)
local appSecret = "APP_SECRET"
local path = "/product/202309/categories"
-- Build the StringToSign
local sb = chilkat.StringBuilder.new()
sb:append(appSecret)
sb:append(path)
local numParams = queryParams.size
local i = 0
while i < numParams do
sb:append(queryParams:name_at(i))
sb:append(queryParams:string_at(i))
i = i + 1
end
sb:append(appSecret)
local crypt = chilkat.Crypt2.new()
crypt.hash_algorithm = "SHA256"
crypt.mac_algorithm = "HMAC"
crypt.encoding_mode = "hex_lower"
crypt:set_mac_key_string(appSecret)
local sig = crypt:mac_string_enc(sb:get_as_string())
queryParams:update_string("access_token", jsonToken:string_of("data.access_token"))
queryParams:update_string("sign", sig)
http:set_request_header("x-tts-access-token", jsonToken:string_of("data.access_token"))
http:set_request_header("content-type", "application/json")
local resp = chilkat.HttpResponse.new()
success = http:http_params("GET", "https://open-api.tiktokglobalshop.com/product/202309/categories", queryParams, resp)
if not success then
print(http.last_error_text)
return
end
local json = chilkat.JsonObject.new()
resp:get_body_json(json)
print(resp.status_code)
local id = ""
local is_leaf = false
local local_name = ""
local parent_id = ""
local j = 0
local count_j = 0
local strVal = ""
local code = json:int_of("code")
local message = json:string_of("message")
local request_id = json:string_of("request_id")
i = 0
local count_i = json:size_of_array("data.categories")
while i < count_i do
json.i = i
id = json:string_of("data.categories[i].id")
is_leaf = json:bool_of("data.categories[i].is_leaf")
local_name = json:string_of("data.categories[i].local_name")
print("local_name: " .. local_name)
parent_id = json:string_of("data.categories[i].parent_id")
j = 0
count_j = json:size_of_array("data.categories[i].permission_statuses")
while j < count_j do
json.j = j
strVal = json:string_of("data.categories[i].permission_statuses[j]")
j = j + 1
end
i = i + 1
end