Swift
Swift
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 Swift Downloads
func chilkatTest() {
var success: Bool = false
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let http = CkoHttp()!
// 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
let jsonToken = CkoJsonObject()!
success = jsonToken.loadFile(path: "qa_data/tokens/tiktok-shops.json")
if success != true {
print("Failed to load tiktok-shops.json")
return
}
// Replace values in all caps with your specific values.
// SHOP_CIPHER and SHOP_ID are returned from this example: Get Authorized Shops
let queryParams = CkoJsonObject()!
queryParams.updateString(jsonPath: "app_key", value: "APP_KEY")
queryParams.updateString(jsonPath: "shop_cipher", value: "SHOP_CIPHER")
queryParams.updateString(jsonPath: "shop_id", value: "SHOP_ID")
let dt = CkoDateTime()!
queryParams.updateString(jsonPath: "timestamp", value: dt.get(asUnixTimeStr: false))
queryParams.updateInt(jsonPath: "version", value: 202309)
// Sort the JSON members by member name, in ascending order (A-Z), case sensitive..
var ascending: Bool = true
var caseSensitive: Bool = true
queryParams.sort(ascending: ascending, caseSensitive: caseSensitive)
var appSecret: String? = "APP_SECRET"
var path: String? = "/product/202309/categories"
// Build the StringToSign
let sb = CkoStringBuilder()!
sb.append(value: appSecret)
sb.append(value: path)
var numParams: Int = queryParams.size.intValue
var i: Int = 0
while i < numParams {
sb.append(value: queryParams.name(at: i))
sb.append(value: queryParams.string(at: i))
i = i + 1
}
sb.append(value: appSecret)
let crypt = CkoCrypt2()!
crypt.hashAlgorithm = "SHA256"
crypt.macAlgorithm = "HMAC"
crypt.encodingMode = "hex_lower"
crypt.setMacKeyString(key: appSecret)
var sig: String? = crypt.macStringENC(inText: sb.getAsString())
queryParams.updateString(jsonPath: "access_token", value: jsonToken.string(of: "data.access_token"))
queryParams.updateString(jsonPath: "sign", value: sig)
http.setRequestHeader(name: "x-tts-access-token", value: jsonToken.string(of: "data.access_token"))
http.setRequestHeader(name: "content-type", value: "application/json")
let resp = CkoHttpResponse()!
success = http.httpParams(verb: "GET", url: "https://open-api.tiktokglobalshop.com/product/202309/categories", json: queryParams, response: resp)
if success == false {
print("\(http.lastErrorText!)")
return
}
let json = CkoJsonObject()!
resp.getBodyJson(json: json)
print("\(resp.statusCode.intValue)")
var id: String?
var is_leaf: Bool
var local_name: String?
var parent_id: String?
var j: Int
var count_j: Int
var strVal: String?
var code: Int = json.int(of: "code").intValue
var message: String? = json.string(of: "message")
var request_id: String? = json.string(of: "request_id")
i = 0
var count_i: Int = json.size(ofArray: "data.categories").intValue
while i < count_i {
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(ofArray: "data.categories[i].permission_statuses").intValue
while j < count_j {
json.j = j
strVal = json.string(of: "data.categories[i].permission_statuses[j]")
j = j + 1
}
i = i + 1
}
}