PureBasic
PureBasic
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 PureBasic Downloads
IncludeFile "CkJsonObject.pb"
IncludeFile "CkDateTime.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkCrypt2.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkHttpResponse.pb"
Procedure ChilkatExample()
success.i = 0
; This example assumes the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.
http.i = CkHttp::ckCreate()
If http.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; 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
jsonToken.i = CkJsonObject::ckCreate()
If jsonToken.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkJsonObject::ckLoadFile(jsonToken,"qa_data/tokens/tiktok-shops.json")
If success <> 1
Debug "Failed to load tiktok-shops.json"
CkHttp::ckDispose(http)
CkJsonObject::ckDispose(jsonToken)
ProcedureReturn
EndIf
; Replace values in all caps with your specific values.
; SHOP_CIPHER and SHOP_ID are returned from this example: Get Authorized Shops
queryParams.i = CkJsonObject::ckCreate()
If queryParams.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(queryParams,"app_key","APP_KEY")
CkJsonObject::ckUpdateString(queryParams,"shop_cipher","SHOP_CIPHER")
CkJsonObject::ckUpdateString(queryParams,"shop_id","SHOP_ID")
dt.i = CkDateTime::ckCreate()
If dt.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(queryParams,"timestamp",CkDateTime::ckGetAsUnixTimeStr(dt,0))
CkJsonObject::ckUpdateInt(queryParams,"version",202309)
; Sort the JSON members by member name, in ascending order (A-Z), case sensitive..
ascending.i = 1
caseSensitive.i = 1
CkJsonObject::ckSort(queryParams,ascending,caseSensitive)
appSecret.s = "APP_SECRET"
path.s = "/product/202309/categories"
; Build the StringToSign
sb.i = CkStringBuilder::ckCreate()
If sb.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkStringBuilder::ckAppend(sb,appSecret)
CkStringBuilder::ckAppend(sb,path)
numParams.i = CkJsonObject::ckSize(queryParams)
i.i = 0
While i < numParams
CkStringBuilder::ckAppend(sb,CkJsonObject::ckNameAt(queryParams,i))
CkStringBuilder::ckAppend(sb,CkJsonObject::ckStringAt(queryParams,i))
i = i + 1
Wend
CkStringBuilder::ckAppend(sb,appSecret)
crypt.i = CkCrypt2::ckCreate()
If crypt.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkCrypt2::setCkHashAlgorithm(crypt, "SHA256")
CkCrypt2::setCkMacAlgorithm(crypt, "HMAC")
CkCrypt2::setCkEncodingMode(crypt, "hex_lower")
CkCrypt2::ckSetMacKeyString(crypt,appSecret)
sig.s = CkCrypt2::ckMacStringENC(crypt,CkStringBuilder::ckGetAsString(sb))
CkJsonObject::ckUpdateString(queryParams,"access_token",CkJsonObject::ckStringOf(jsonToken,"data.access_token"))
CkJsonObject::ckUpdateString(queryParams,"sign",sig)
CkHttp::ckSetRequestHeader(http,"x-tts-access-token",CkJsonObject::ckStringOf(jsonToken,"data.access_token"))
CkHttp::ckSetRequestHeader(http,"content-type","application/json")
resp.i = CkHttpResponse::ckCreate()
If resp.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkHttp::ckHttpParams(http,"GET","https://open-api.tiktokglobalshop.com/product/202309/categories",queryParams,resp)
If success = 0
Debug CkHttp::ckLastErrorText(http)
CkHttp::ckDispose(http)
CkJsonObject::ckDispose(jsonToken)
CkJsonObject::ckDispose(queryParams)
CkDateTime::ckDispose(dt)
CkStringBuilder::ckDispose(sb)
CkCrypt2::ckDispose(crypt)
CkHttpResponse::ckDispose(resp)
ProcedureReturn
EndIf
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkHttpResponse::ckGetBodyJson(resp,json)
Debug Str(CkHttpResponse::ckStatusCode(resp))
id.s
is_leaf.i
local_name.s
parent_id.s
j.i
count_j.i
strVal.s
code.i = CkJsonObject::ckIntOf(json,"code")
message.s = CkJsonObject::ckStringOf(json,"message")
request_id.s = CkJsonObject::ckStringOf(json,"request_id")
i = 0
count_i.i = CkJsonObject::ckSizeOfArray(json,"data.categories")
While i < count_i
CkJsonObject::setCkI(json, i)
id = CkJsonObject::ckStringOf(json,"data.categories[i].id")
is_leaf = CkJsonObject::ckBoolOf(json,"data.categories[i].is_leaf")
local_name = CkJsonObject::ckStringOf(json,"data.categories[i].local_name")
Debug "local_name: " + local_name
parent_id = CkJsonObject::ckStringOf(json,"data.categories[i].parent_id")
j = 0
count_j = CkJsonObject::ckSizeOfArray(json,"data.categories[i].permission_statuses")
While j < count_j
CkJsonObject::setCkJ(json, j)
strVal = CkJsonObject::ckStringOf(json,"data.categories[i].permission_statuses[j]")
j = j + 1
Wend
i = i + 1
Wend
CkHttp::ckDispose(http)
CkJsonObject::ckDispose(jsonToken)
CkJsonObject::ckDispose(queryParams)
CkDateTime::ckDispose(dt)
CkStringBuilder::ckDispose(sb)
CkCrypt2::ckDispose(crypt)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(json)
ProcedureReturn
EndProcedure