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

Lua
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

local queryParams = chilkat.JsonObject.new()
queryParams:update_string("app_key", "APP_KEY")
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 = "/authorization/202309/shops"

-- 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/authorization/202309/shops", 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)
print(resp.body_str)

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

local cipher = ""
local code_str = ""
local id = ""
local name = ""
local region = ""
local seller_type = ""

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.shops")
while i < count_i do
    json.i = i
    cipher = json:string_of("data.shops[i].cipher")
    code_str = json:string_of("data.shops[i].code")
    id = json:string_of("data.shops[i].id")
    name = json:string_of("data.shops[i].name")
    region = json:string_of("data.shops[i].region")
    seller_type = json:string_of("data.shops[i].seller_type")
    i = i + 1
end